Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: How to link against a static 3rd party lib?

What I have: my code (simple main.cpp), headers of the 3rd party lib (EnvVar TPLIB_INCLUDE), binary lib (several .a files in TPLIB_BINARY_PATH) and the following CMakeLists.txt:

# current source directory: CMAKE_CURRENT_SOURCE_DIR
# current binary directory: CMAKE_CURRENT_BINARY_DIR

# require 2.6 to get support for the simple IF construct
CMAKE_MINIMUM_REQUIRED(VERSION 2.6.0)

PROJECT( simpleapp)
SET( PROGNAME simpleapp )

SET( CMAKE_COLOR_MAKEFILE ON )
SET( CMAKE_VERBOSE_MAKEFILE ON )
SET( CMAKE_INCLUDE_CURRENT_DIR TRUE )

# the actual .cpp files go here
SET(project_SOURCES
    main.cpp
)

# add here all files that need processing by Qt's MOC if there are any
set(project_MOC_SOURCES
    # files would go here
)

# add here all files that will be processed by Qt's UIC
set(project_UIS
    # all .ui files would go here
)

# additional Qt resources go here
set(project_RCCS
    # all .qrc files would go here
)

# setup Qt
FIND_PACKAGE(Qt4 REQUIRED)

INCLUDE(${QT_USE_FILE})

QT4_WRAP_CPP(test_MOCS ${project_MOC_SOURCES})
QT4_WRAP_UI(test_UIS_H ${project_UIS})
QT4_WRAP_CPP(test_MOC_UI ${project_UIS_H})
QT4_ADD_RESOURCES(test_RCC_SRCS ${project_RCCS})

include_directories( ${CMAKE_BINARY_DIR} $ENV{TPLIB_INCLUDE})

ADD_EXECUTABLE( ${PROGNAME} ${project_SOURCES} ${test_MOCS} ${test_RCC_SRCS} ${test_MOC_UI} )

link_directories($ENV{TPLIB_BINARY_PATH})

TARGET_LINK_LIBRARIES(${PROGNAME} ${QT_LIBRARIES} lib_misc lib_tools)

makeing the project creating the main.cpp.o works as usual. Thus CMake found the third party headers. When it comes to link ld complains cannot find -llib_misc and -llib_tools. Removing the "link_directories" line or specifiingy the libs absolute name leads to "No rule to make target /path/to/lib_misc.a"

So how to tell CMake to use these libs for linking only?

like image 492
mbx Avatar asked Aug 14 '11 20:08

mbx


People also ask

How do I add a shared library to CMake?

You already know the location of your library. Add it to the CMAKE_PREFIX_PATH when you call CMake, then CMake will look for your library in the passed paths, too. If you don't add one of these visibility specifiers, it will either behave like PRIVATE or PUBLIC , depending on the CMake version and the policies set. 3.

How do I link a .so file in CMake?

Just do TARGET_LINK_LIBRARIES(program /path/to/library.so) > > > > 2. Better one is to: > > > > FIND_LIBRARY(MY_LIB NAMES library > > PATHS /path/to) > > TARGET_LINK_LIBRARIES(program ${MY_LIB}) > > > > Andy > > > > On Fri, 2004-10-29 at 10:19, Ning Cao wrote: > >> I want to link a .


1 Answers

link_directories($ENV{TPLIB_BINARY_PATH}) should be placed before ADD_EXECUTABLE.

From the official documents of CMake, there are notes on link_directories:

The command will apply only to targets created after it is called.

like image 118
Yantao Xie Avatar answered Nov 14 '22 08:11

Yantao Xie