Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake link an external library

Tags:

cmake

First of all, I am newbie in CMake. I've just started work with it. I want to link an external library to my project. I use code which I take from CMake wiki (at the end of article). Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

project(hello_world)

set(SOURCE_EXE main.cpp)

include_directories(foo)

add_library(foo STATIC IMPORTED)
set_property(TARGET foo PROPERTY IMPORTED_LOCATION /usr/lib/libfoo.a)

target_link_libraries(main foo)

And here is a text of error:

-- The C compiler identification is GNU 4.7.3
-- The CXX compiler identification is GNU 4.7.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Error at CMakeLists.txt:24 (target_link_libraries):
  Cannot specify link libraries for target "main" which is not built by this
  project.


-- Configuring incomplete, errors occurred!

How can I do it correctly?

like image 588
desperius Avatar asked Jun 12 '13 16:06

desperius


1 Answers

It looks like you're just missing out an add_executable call. You need to add main as an executable target in your CMakeLists.txt:

set(SOURCE_EXE main.cpp)
add_executable(main ${SOURCE_EXE})
...
target_link_libraries(main foo)
like image 112
Fraser Avatar answered Sep 24 '22 01:09

Fraser