6

When I try to import a library using

 add_library(libname SHARED IMPORTED)
    set_property(TARGET libname PROPERTY IMPORTED_LOCATION /<foldername>/<sub-foldername>/lib")

The cmake shouts :

CMake Warning (dev) at /CMakeLists.txt:28 (target_link_libraries): Cannot specify link libraries for target "libname" which is not built by this project.

CMake does not support this but it used to work accidentally and is being allowed for compatibility.

Policy CMP0016 is not set: target_link_libraries() reports error if only argument is not a target. Run "cmake --help-policy CMP0016" for policy details. Use the cmake_policy command to set the policy and suppress this warning. This warning is for project developers. Use -Wno-dev to suppress it.

If this is true, what is the other best way to include a library somewhere in my build tree into another project. I have a library setup and another place has executable which will be using the libraries. Reading through the cmake documentation, it felt like this would be the best way forward but seems its a broken piece which is just being supported.

navderm
  • 799
  • 2
  • 11
  • 33
  • I think it would be helpful to elaborate on your project setup: for example the directory structure for the executable and library, as well as the CMakeLists that are in play. – ajmccluskey Apr 11 '13 at 05:20
  • Why do you need the library as target? Can you not just use `target_link_libraries(EXECUTABLE ///lib)`? – Jan Rüegg Jul 03 '13 at 10:23

2 Answers2

14

Cannot specify link libraries for target "libname" which is not built by this project

When you use target_link_libraries to some target you're specifying how to build it, but imported library is already build. CMake told you that...

Example of linking imported target to executable:

add_library(boo SHARED IMPORTED)
set_target_properties(boo PROPERTIES IMPORTED_LOCATION "/path/to/boo/library")
add_executable(foo foo.cpp)
target_link_libraries(foo boo)

Note: using imported targets

JDQ
  • 443
  • 7
  • 11
  • 7
    there is a `GLOBAL` option, which allows to use the imported library in directories above the current: `add_library(breakpad STATIC IMPORTED GLOBAL)` – Roman Kruglov Feb 16 '16 at 15:38
3

I was getting the same error as navderm when trying to import the Poco C++ libPocoFoundation.so library into my project, and after trying different solutions unsuccessfully, I managed to find one that worked for me:

cmake_minimum_required(VERSION 3.5)
project(MyProject)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

add_library(PocoLib SHARED IMPORTED GLOBAL)

# It's important to specify the full path to the library you want to import
set_target_properties(PocoLib PROPERTIES IMPORTED_LOCATION "/usr/local/lib/Poco_1.7.2/lib/libPocoFoundation.so")

# create my executable
set(EXEC_SOURCE_FILES main.cpp)
add_executable(MyProject ${EXEC_SOURCE_FILES})

target_link_libraries(MyProject PocoLib)
natella
  • 31
  • 1