microsoft / cpprestsdk

The C++ REST SDK is a Microsoft project for cloud-based client-server communication in native code using a modern asynchronous C++ API design. This project aims to help C++ developers connect to and interact with services.
Other
8k stars 1.65k forks source link

Build error on Linux (Ubuntu 16.04) #559

Open abhijeetgaiha opened 7 years ago

abhijeetgaiha commented 7 years ago

../../Binaries/libcpprest.so.2.9: undefined reference to dladdr' ../../Binaries/libcpprest.so.2.9: undefined reference todlopen' ../../Binaries/libcpprest.so.2.9: undefined reference to dlclose' ../../Binaries/libcpprest.so.2.9: undefined reference todlerror' ../../Binaries/libcpprest.so.2.9: undefined reference to `dlsym' collect2: error: ld returned 1 exit status samples/SearchFile/CMakeFiles/SearchFile.dir/build.make:106: recipe for target 'Binaries/SearchFile' failed make[2]: [Binaries/SearchFile] Error 1 CMakeFiles/Makefile2:1079: recipe for target 'samples/SearchFile/CMakeFiles/SearchFile.dir/all' failed make[1]: [samples/SearchFile/CMakeFiles/SearchFile.dir/all] Error 2 Makefile:138: recipe for target 'all' failed make: *** [all] Error 2

abhijeetgaiha commented 7 years ago

A suggested fix to add -ldl to the linker options but there is no editable makefile for this project.

mobileben commented 7 years ago

Look at #443. I created a pull request to fix this though at this point I doubt MS will incorporate it.

ras0219-msft commented 7 years ago

Thanks for reporting this issue!

I don't see where libcpprest could be pulling in dlopen and dlerror:

cpprestsdk/Release$ grep -R dlopen
tests/common/TestRunner/test_module_loader.cpp:        m_handle = dlopen(path.c_str(), RTLD_LAZY|RTLD_GLOBAL);
cpprestsdk/Release$ grep -R dlerror
tests/common/TestRunner/test_module_loader.cpp:                " " << dlerror() <<
tests/common/TestRunner/test_module_loader.cpp:            std::cerr << std::string(dlerror()) << std::endl;
tests/common/TestRunner/test_module_loader.cpp:            std::cerr << std::string(dlerror()) << std::endl;

Note that all these occurrences are inside the test runner, not inside libcpprest.so. However, I do notice we might not be linking CMAKE_DL_LIBS to the test_runner on OSX, so I'll fix that.

Can you supply your system information and a full description of how to reproduce the problem?

Southclaws commented 6 years ago

I'm running into this as well on a Debian image, if it helps I have:

RitaCaoCN commented 6 years ago

you can try to add -ldl to the end of "./samples/SearchFile/CMakeFiles/SearchFile.dir/link.txt"

cirvine-MSFT commented 5 years ago

Adding some notes here since I've been hitting this issue in our project that uses cpprestsdk:

cpprestsdk depends on openssl which depends on dl for dl* methods like dlopen. We hit this issue when building cpprestsdk test or sample code, but not the product code. Presumably this is becuase it needs to link a final executable for the tests/samples. This may only happen when building static libs. e.g. -DBUILD_SHARED_LIBS=0 We can disable building this code with -DBUILD_TESTS=OFF and -DBUILD_SAMPLES=OFF.

However, we still get the same undefined reference errors in our final executable, because it needs to link to dl for those same methods. I'm still working on getting dl linked to our executable.

Edit: I'm also pretty sure I'm only seeing this as an issue with openssl 1.0.2 and not 1.1.0. I started hitting this error when I tried to downgrade my openssl version from 1.0.2 to 1.1.0.

Edit2: I'm 99% sure this only happens when statically linking cpprestsdk, but not statically linking openssl for cpprestsdk. Curious if @ras0219-msft has thoughts or can correct me here.

We were able to fix this issue in our code by doing the equivalent of:

target_link_libraries (${TARGETNAME} PUBLIC cpprestsdk::cpprest)
if (UNIX)
    set(CMAKE_PREFIX_PATH /usr/arm-linux-gnueabihf)
    find_library (DL_LIB NAMES dl)
    target_link_libraries (${TARGETNAME} PUBLIC ${DL_LIB})
endif ()
kyo055 commented 5 years ago

I built the library and ran the test program OK. But when I tried to compile my own program with this problem : g++ -std=c++11 test.cpp -o test -L. -lboost_system -lcrypto -lssl -lcpprest -I../../include test.cpp:37:5: warning: second argument of ‘int main(int, char*)’ should be ‘char **’ [-Wmain] int main(int argc, char argv[]) ^ ./libcpprest.so: undefined reference to SSLv2_client_method' ./libcpprest.so: undefined reference toSSLv2_server_method' ./libcpprest.so: undefined reference to `SSLv2_method' collect2: error: ld returned 1 exit status