How to add “-l” (ell) compiler flag in CMake

前端 未结 2 987
长发绾君心
长发绾君心 2020-11-22 00:16

Work on Ubuntu 16

I used g++ main.cpp -lpq command for compiler my small project. Now I use Clion and wanna do same what I do with g++

2条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 01:12

    -lq is not a compiler flag (CFLAGS) but a linker flag.

    To pass a library in a CMake project you should use:

    target_link_libraries(target_name libraries...)
    

    Note that if you specify 'q' as library the project will link with libq.a or, if you are on windows q.dll.

    ... in your CMakeLists.txt the correct line to add is:

    target_link_libraries(day_g pq)
    

    Note also that when you add a CFLAG you should also "remember" the previous ones that may be added by libraries or by your platform, ie:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
    

    To check the exact flags cmake is passing to compiler or linker you can always run, from the build directory, the following command:

    make VERBOSE=1
    

提交回复
热议问题