CMAKE_CXX_FLAGS中的标志"-l"不起作用 [英] Flag '-l' in CMAKE_CXX_FLAGS doesn't work
问题描述
我在Mac机器上编写了一些代码,它运行良好,但是当我将其移植到Linux机器时,我得到了undefined reference to curl_easy_init
I have some code I wrote on my mac machine and it has been working perfectly but when I port it over to a Linux machine I get an undefined reference to curl_easy_init
我的编译器标志包括用于链接的-lcurl
.
My compiler flags include a -lcurl
for linking.
这是我的链接方式:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -L/curl/lib/dir -lcurl")
我尝试过使用-L/curl/lib/dir
此计算机上已安装卷曲:
Curl is installed on this machine:
$ curl --version
curl 7.47.0 (x86_64-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets
推荐答案
从不将-l
标志添加到 CMAKE_EXE_LINKER_FLAGS ,此外还添加到 CMAKE_CXX_FLAGS (标志-l
用于链接器,而不用于编译器).
Never add -l
flags to CMAKE_EXE_LINKER_FLAGS and moreover to CMAKE_CXX_FLAGS (the flag -l
is for the linker, not for a compiler).
要与图书馆建立链接,请使用 target_link_libraries :专为此目的:
For link with libraries use target_link_libraries: it is specifically intended for that purpose:
target_link_libraries(<your-executable> curl)
将标记添加到*_FLAGS
变量时,该标记会在链接器的命令行中的源文件之前(实际上是目标文件)之前添加.如果源文件使用库中的某些功能,则链接器无法找到它.
When you add a flag to *_FLAGS
variable, the flag is added before the source file (object file actually) in the linker's command-line. If the source file uses some function from the library, then the linker cannot find it.
相反,在链接器的命令行中,在源文件后后添加了由命令target_link_libraries
生成的标志.
As opposite, a flag produced by command target_link_libraries
is added after the source file in the linker's command line.
这篇关于CMAKE_CXX_FLAGS中的标志"-l"不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!