简体   繁体   中英

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

I have an existing project that was on Windows and Linux. I recently got a mac for the first time and I am trying to set it up for C++ development but I'm having an issue linking to curl I believe.

From what I've seen, curl supports the M1 arm based chip via homebrew which I installed using homebrew install curl .

Below is my make file

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

When I run make, I get the following:

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

I've tried setting the -arch to x86_64 as a test on on the LDFLAGS and it then successfully compiles and creates the library, but I don't really see that I should need to build x86_64 when it should be built for M1, and therefore be quicker as it won't have to go through Rosetta.

If I run lipo -info /opt/homebrew/opt/curl/bin/curl to check the architecture of the library I get the following:

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

So curl looks to be correct so not sure why I'm getting error undefined symbols for arm64

I don't see any reference to the curl library in your makefile. To rectify this, you (probably) need to add -lcurl to your LDFLAGS .

Also, /opt/homebrew/opt/curl/bin/curl is the curl executable, not the library. That is (probably) /opt/homebrew/opt/curl/lib/libcurl.so

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM