在 M1 Mac 上从 C++ 链接到 curl - arm64 的未定义符号

时间:2021-05-16 15:21:57

标签: c++ macos curl apple-m1

我有一个在 Windows 和 Linux 上运行的现有项目。我最近第一次拿到了 mac,我正在尝试将它设置为 C++ 开发,但我相信我在链接到 curl 时遇到了问题。

据我所知,curl 通过我使用 homebrew install curl 安装的自制软件支持基于 M1 arm 的芯片。

下面是我的make文件

SOURCES = DataDogStatsD.cpp DDEvent.cpp Helpers.cpp

lib_name = libDataDogStatsD.so.1.1.0.5

curl_include = /usr/local/include/curl
rapidjson_inc_path = /usr/local/include/rapidjson

OBJECTS = $(SOURCES:.cpp=.o)
CFLAGS = -fpic -c $(SOURCES) -Wall -g -Iinclude -std=c++11 -I/usr/include -I$(curl_include) -I$(rapidjson_inc_path)
CC = g++
LDFLAGS = -lpthread -pthread -lm -L/opt/homebrew/opt/curl/lib

.PHONY: clean

default:
    $(CC) -shared -Wl,-install_name,libDataDogStatsD.so.1 -o $(lib_name) $(OBJECTS) $(LDFLAGS)
    ln -sf $(lib_name) libDataDogStatsD.so

clean:
    rm -vf $(OBJECTS) depend $(lib_name)

depend: $(SOURCES)
    $(CC) $(CFLAGS) > depend

-include depend

当我运行 make 时,我得到以下信息:

g++ -fpic -c DataDogStatsD.cpp DDEvent.cpp Helpers.cpp -Wall -g -Iinclude -std=c++11 -I/usr/include -I/usr/local/include/curl -I/usr/local/include/rapidjson > depend
g++ -shared -Wl,-install_name,libDataDogStatsD.so.1 -o libDataDogStatsD.so.1.1.0.5 DataDogStatsD.o DDEvent.o Helpers.o -lpthread -pthread -lm -L/opt/homebrew/opt/curl/lib
Undefined symbols for architecture arm64:
  "_curl_easy_cleanup", referenced from:
      DataDogStatsD::event(DDEvent, bool, void (*)(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)) in DataDogStatsD.o
      DataDogStatsD::sendDDEventinthread(DDEvent, void (*)(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)) in DataDogStatsD.o
  "_curl_easy_getinfo", referenced from:
      DataDogStatsD::event(DDEvent, bool, void (*)(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)) in DataDogStatsD.o
      DataDogStatsD::sendDDEventinthread(DDEvent, void (*)(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)) in DataDogStatsD.o
  "_curl_easy_init", referenced from:
      DataDogStatsD::initCurl(DDEvent, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, curl_slist*, char const*) in DataDogStatsD.o
  "_curl_easy_perform", referenced from:
      DataDogStatsD::event(DDEvent, bool, void (*)(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)) in DataDogStatsD.o
      DataDogStatsD::sendDDEventinthread(DDEvent, void (*)(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)) in DataDogStatsD.o
  "_curl_easy_setopt", referenced from:
      DataDogStatsD::initCurl(DDEvent, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, curl_slist*, char const*) in DataDogStatsD.o
  "_curl_easy_strerror", referenced from:
      DataDogStatsD::event(DDEvent, bool, void (*)(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)) in DataDogStatsD.o
      DataDogStatsD::sendDDEventinthread(DDEvent, void (*)(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)) in DataDogStatsD.o
  "_curl_slist_append", referenced from:
      DataDogStatsD::initCurl(DDEvent, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, curl_slist*, char const*) in DataDogStatsD.o
  "_curl_slist_free_all", referenced from:
      DataDogStatsD::event(DDEvent, bool, void (*)(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)) in DataDogStatsD.o
      DataDogStatsD::sendDDEventinthread(DDEvent, void (*)(bool, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)) in DataDogStatsD.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [default] Error 1

我已经尝试将 -arch 设置为 x86_64 作为对 LDFLAGS 的测试,然后它成功编译并创建了库,但我真的没有看到我应该在构建 x86_64 时构建它M1,因此速度更快,因为它不必通过 Rosetta。

如果我运行 lipo -info /opt/homebrew/opt/curl/bin/curl 来检查库的架构,我会得到以下信息:

Non-fat file: /opt/homebrew/opt/curl/bin/curl is architecture: arm64

所以 curl 看起来是正确的,所以不知道为什么我收到 arm64 的错误未定义符号

1 个答案:

答案 0 :(得分:0)

我在您的 makefile 中没有看到对 curl 库的任何引用。要纠正此问题,您(可能)需要将 -lcurl 添加到您的 LDFLAGS

此外,/opt/homebrew/opt/curl/bin/curl 是 curl 可执行文件,而不是库。那是(可能)/opt/homebrew/opt/curl/lib/libcurl.so