Link library with cmake

2019-08-08 15:26发布

问题:

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.

回答1:

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 and here and here are some examples and since CMake has good documentation you should read more about the commands here.

And cmakelists.txt files should be named CMakeLists.txt



标签: cmake