when my application need link pthread library,i write set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lpthread")
in CmakeLists.txt and it works.but,when i need link dl lib, set ( CMAKE_C_FLAGS_C_FLAGS "${CMAKE_C_FLAGS} -ldl")
does not work.if i use gcc directly,gcc sqlite3.c shell.c -lpthread -ldl
works perfectly.after google,i get that target_link_libraries(MY_TARGET LIB1 LIB2 ... LIBN ${CMAKE_DL_LIBS})
works.i tried.i did work.i want to know why set(CMAKE_C_FLAGS...)
does not work?
Asked
Active
Viewed 1,041 times
-1

liu
- 181
- 1
- 10
-
Is `CMAKE_C_FLAGS_C_FLAGS` (instead of just `CMAKE_C_FLAGS`) intentional? – 0x5453 Nov 01 '19 at 16:05
-
sorry,it is a clerical error.i have corrected it. – liu Nov 01 '19 at 16:12
-
For maximum portability you should use `set(CMAKE_THREAD_PREFER_PTHREAD 1)` followed by `find_package(Threads REQUIRED)` and later call `target_link_libraries(MY_TARGET LIB1 LIB2 ... LIBN Threads::Threads)` This adds the necessary switches to the compiler and the thread library including its dependencies to the linker. – vre Nov 01 '19 at 17:06
1 Answers
2
It seems you are trying to link against the dl library. However, CMAKE_C_FLAGS
only affect compiling, not linking. Maybe you rather want to set CMAKE_*_LINKER_FLAGS
(the linker flags are separated for EXE
, SHARED
and MODULE
).
However the modern way to declare the used libraries is the target_link_libraries
command, as you've mentioned.
Here are the respective links to the documentation.
-
thank you.so,`set ( CMAKE_C_FLAGS_C_FLAGS "${CMAKE_C_FLAGS} -ldl")` is different from ` gcc sourcefile.c -ldl`, assume that my compiler is gcc.Is it? – liu Nov 03 '19 at 01:03