Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

c++ - Flag '-l' in CMAKE_CXX_FLAGS doesn't work

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 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...