warwcat warwcat - 6 months ago 38
C++ Question

Cmake import -lpthread and others

Maybe its a stupid question but I have an program that I need -lmysqlcppconn -lreadline and -lpthread.

I'm using the cmake to generate the makefile for that I resolve use :

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -lmysqlcppconn -lreadline -lpthread")
...(other command most likely irrelevant)

add_executable(name ${SOURCES} ${CMAKE_CXX_FLAG})

but I received the follow warning when I execute the makefile :

clang: warning: -lmysqlcppconn: 'linker' input unused
clang: warning: -lreadline: 'linker' input unused
clang: warning: -lpthread: 'linker' input unused

Exists a better way to do it?


CMAKE_CXX_FLAGS is for compiler flags (with CMake-generated make files compilation and linking are separate steps). To link you need something like

target_link_libraries(name mysqlcppconn)
target_link_libraries(name readline)
target_link_libraries(name pthread)

For threading library a better way is to follow the process explained in this answer