如何添加“ -l” (ell)CMake中的编译器标志 [英] How to add "-l" (ell) compiler flag in CMake
问题描述
在Ubuntu 16上工作
Work on Ubuntu 16
我用 g ++ main.cpp -lpq
命令来编译我的小项目。现在,我使用 Clion 并想像使用 g ++
一样。但是我不能在 cmake 文件中添加编译器标志并获得编译错误。
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++
. But I can't add compiler flags in cmake file and get compile error.
cmake_minimum_required(VERSION 3.5.1)
project(day_g)
set(CMAKE_CXX_FLAGS "-lpq")
add_definitions(-lpq)
message("CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(day_g ${SOURCE_FILES})
我也只运行cmake文件并使用 -lpq 标志获取 CMAKE_CXX_FLAGS
。
Also I run only cmake file and get CMAKE_CXX_FLAGS
with -lpq flag.
CMAKE_CXX_FLAGS is -lpq
-- Configuring done
-- Generating done
如何正确向cmake文件添加编译器标志?
How properly add compiler flags to cmake file?
推荐答案
标记 -l </ code>用于链接器,而不用于编译器。该标志用于与库的链接。 CMake为此具有特殊命令 target_link_libraries :
Flag -l
is for linker, not for compiler. This flag is used for link with libraries. CMake has special command target_link_libraries for that purpose:
target_link_libraries(day_g pq)
这篇关于如何添加“ -l” (ell)CMake中的编译器标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!