Gaurav Maan
Gaurav Maan

Reputation: 349

Unable to include external previous built .a libraries in cmake

I have a libgarithm.a library previously compiled and have a header file garith.h how could i import it in my cmake project . I have included header files from include_directories("/home/gaurav/Desktop/garith-lib/include") but unable to link libraries and it is giving a comile time error --- undefined reference to `multi(int, int)' the function in my library

Upvotes: 1

Views: 749

Answers (2)

Adnan Y
Adnan Y

Reputation: 3240

Starting with CMake 3.12.1, target_include_directories as well as other syntactic sugar for normal targets is also supported with imported targets.

For those writing cmake files with these statements, please do add cmake_minimum_required(3.12)

If you are getting errors such when compiling a particular third party library, check the cmake --version

Upvotes: 0

rocambille
rocambille

Reputation: 15986

You should create an imported target for your library and then use target_link_libraries:

add_library(garithm STATIC IMPORTED)
set_property(TARGET garithm PROPERTY IMPORTED_LOCATION
    /path/to/libgarithm.a
)
set_property(TARGET garithm PROPERTY INTERFACE_INCLUDE_DIRECTORIES
    /home/gaurav/Desktop/garith-lib/include
)

...

add_executable(foo main.cpp)
target_link_libraries(foo garithm)

Include directories are declared on the imported target as well so you don't have to call include_directories

EDIT: target_include_directoriesdoesn't work with imported targets, set the property INTERFACE_INCLUDE_DIRECTORIES instead

Upvotes: 2

Related Questions