c++ - Curl 与 Catalina C++ 失败并获得 "Undefined symbols for architecture x86_64"

标签 c++ macos curl libcurl

我正在尝试使用 Catalina 10.15 运行示例 C++ 程序:

#include </usr/local/opt/curl/include/curl/curl.h>

int main(void) {

    CURL* curl;
    CURLcode result;

    curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.wikipedia.org");

    result = curl_easy_perform(curl);

    curl_easy_cleanup(curl);

    return 0;
}

我已经安装了 curl :
brew install curl

我不明白为什么这不起作用,我得到:
Scanning dependencies of target 2700
[ 50%] Building CXX object CMakeFiles/2700.dir/main.cpp.o
[100%] Linking CXX executable 2700
Undefined symbols for architecture x86_64:
  "_curl_easy_cleanup", referenced from:
      _main in main.cpp.o
  "_curl_easy_init", referenced from:
      _main in main.cpp.o
  "_curl_easy_perform", referenced from:
      _main in main.cpp.o
  "_curl_easy_setopt", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [2700] Error 1
make[2]: *** [CMakeFiles/2700.dir/all] Error 2
make[1]: *** [CMakeFiles/2700.dir/rule] Error 2
make: *** [2700] Error 2

我的 CMakeLists.txt 如下:
cmake_minimum_required(VERSION 3.15.3)
project(2700)

include_directories(/usr/local/opt/curl/include/)

set(CMAKE_CXX_STANDARD 17)

add_executable(2700 main.cpp )

我正在使用 CLion 并且我对 C++ 相当陌生,尤其是在 MacOS 上。

我该怎么做才能让 curl 正常工作而不出现问题?

最佳答案

我意识到我需要在 CMakeLists.txt 中进行链接:

这有效:

cmake_minimum_required(VERSION 3.15.3)
project(2700)

include_directories(/usr/local/opt/curl/include/)

set(CMAKE_CXX_STANDARD 17)

set(CURL_LIBRARY "-lcurl")
find_package(CURL REQUIRED)

add_executable(2700 main.cpp )

include_directories(${CURL_INCLUDE_DIR})
target_link_libraries(2700 ${CURL_LIBRARIES})

关于c++ - Curl 与 Catalina C++ 失败并获得 "Undefined symbols for architecture x86_64",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59923282/

相关文章:

php - 使用 curl 在电报机器人中发送消息

Python3-LDAP 未返回预期结果

iphone - Objective-C 中的非原子和只读属性

c++ - std::string s1 {"Modern C++", 3} 与 std::string s1 {str, 3}

c++ - 降低 Unreal Engine 4.22 上 C++ 的时间复杂度

c++ - 我应该在移动构造函数中 std::move 一个 shared_ptr 吗?

swift - 如何在 Process() 中包含命令修饰符?

node.js - 复制大文件(超过 2GB)时出现 Node 未知系统错误

c - 使用 libcurl 链接程序时未解析的符号

c++ - OpenCV 处理图像的一部分