[CMake] How can link to a library just like the system libs?

Philip Lowman philip at yhbt.com
Sat Mar 14 12:06:17 EDT 2009


On Sat, Mar 14, 2009 at 10:43 AM, Kermit Mei <kermit.mei at gmail.com> wrote:

> Hello, I have a library which had been installed into my system
> under /usr/lib/. When I manually compile it, I must type:
> cc -Wall udpcli.c -o cli -lapue
>
> Now, I wrote my cmakelists.txt like this:
> CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
> PROJECT(UDPCS)
> FIND_LIBRARY(APUE_LIB, /usr/lib/)
> ADD_EXECUTABLE(serv udpserv.c)
> ADD_EXECUTABLE(cli udpcli.c)
> TARGET_LINK_LIBRARIES(cli, ${APUE_LIB})
>
> And then I encounter the following errors:
> $ cmake -DBUILD_TYPE=debug ..
> CMake Error: Attempt to add link library "/usr/lib/libapue.so" to target
> "cli," which is not built by this project.
> -- Configuring done
>
> How can I settle it?
>

The code above has many problems with it.  For one, there should be no
commas.  Secondly, your find_library() call is missing the name of the
library, it instead contains a directory name.  I believe you can use full
paths to libraries in the find_library() call for the library name, however
this is generally a bad idea.  You should read the documentation for
find_library() to understand how it works.  "/usr/lib" is searched
automatically as it is a system path.  Usually all that's needed is the
library name (without the "lib" part).

The error message you mention most commonly happens when you call
target_link_libraries() prior to calling add_executable() or add_library().
I suspect that's what you did.

Here's what you probably want:

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(UDPCS)
FIND_LIBRARY(APUE_LIB apue)
ADD_EXECUTABLE(serv udpserv.c)
ADD_EXECUTABLE(cli udpcli.c)
TARGET_LINK_LIBRARIES(cli ${APUE_LIB})

Also, consider using lowercase command names for new CMake code.  It works
the same and is a lot easier on the eyes:

cmake_minimum_required(VERSION 2.6)
project(UDPCS)
find_library(APUE_LIB apue)
add_executable(serv udpserv.c)
add_executable(cli udpcli.c)
target_link_libraries(cli ${APUE_LIB})

-- 
Philip Lowman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090314/5cccff16/attachment.htm>


More information about the CMake mailing list