user2023431
user2023431

Reputation: 41

Link library with cmake

I installed the library bcm2835 on my pc. To compile a program in c I must type:

gcc -o my_program my_program.c -l rt -l bcm2835 

Now I must compile another program that use the same libraries with cmake. I have never used this. What must I add to the bottom of "cmakelists.txt"?

I have tried:

TARGET_LINK_LIBRARIES(my_program rt)  
TARGET_LINK_LIBRARIES(my_program bcm2835)

but that doesn't work.

Upvotes: 4

Views: 2484

Answers (1)

Domen Vrankar
Domen Vrankar

Reputation: 1743

cmake_minimum_required (VERSION 2.6)
project( my_program )

find_library( rt_lib rt OTHER_PARAMETERS_THAT_YOU_REQUIRE_SEE_DOCUMENTATION_LINK )
find_library( bcm2835_lib bcm2835 OTHER_PARAMETERS_THAT_YOU_REQUIRE_SEE_DOCUMENTATION_LINK )

include_directories( LIST_OF_REQUIRED_INCLUDE_DIRECTORIES_SEE_DOCUMENTATION_LINK ) 

add_executable( my_program my_program.c )
target_link_libraries( my_program rt_lib bcm2835_lib )

Here are some examples:

Since CMake has good documentation, you should read more about the commands in the latest CMake documentation

Also, cmakelists.txt files should be named CMakeLists.txt.

Upvotes: 8

Related Questions