Flag '-l' in CMAKE_CXX_FLAGS doesn't work

爱⌒轻易说出口 提交于 2019-12-18 09:06:58

问题


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

My compiler flags include a -lcurl for linking.

Here's how I'm linking:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -L/curl/lib/dir -lcurl")

I've tried with and without the -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 

回答1:


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).

For link with libraries use target_link_libraries: it is specifically intended for that purpose:

target_link_libraries(<your-executable> curl)

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.

As opposite, a flag produced by command target_link_libraries is added after the source file in the linker's command line.



来源:https://stackoverflow.com/questions/48968639/flag-l-in-cmake-cxx-flags-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!