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

Linking to curl from C++ on M1 Mac - undefined symbols for arm64

我有一个在 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 的未定义符号错误

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

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