本文介绍了CMAKE_CXX_FLAGS 中的标志“-l"不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 mac 机器上写了一些代码,它运行得很好,但是当我把它移植到 Linux 机器上时,我得到一个 对 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 安装在这台机器上:

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

推荐答案

Never-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"不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:20