[英]How to address undefined reference error in c++ for simple multifile compilation
I'm trying to try out the cpprestsdk library and am finding that when compiling using clang++ src/handler.cpp main.cpp
, I get this error:我正在尝试尝试 cpprestsdk 库,并发现在使用clang++ src/handler.cpp main.cpp
进行编译时,出现此错误:
/usr/bin/ld: /tmp/handler-8f246d.o: in function `boost::asio::error::detail::ssl_category::message[abi:cxx11](int) const':
handler.cpp:(.text._ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei[_ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei]+0x29): undefined reference to `ERR_reason_error_string'
/usr/bin/ld: /tmp/handler-8f246d.o: in function `boost::asio::detail::posix_thread::join()':
handler.cpp:(.text._ZN5boost4asio6detail12posix_thread4joinEv[_ZN5boost4asio6detail12posix_thread4joinEv]+0x2a): undefined reference to `pthread_join'
/usr/bin/ld: /tmp/handler-8f246d.o: in function `boost::asio::detail::posix_thread::~posix_thread()':
handler.cpp:(.text._ZN5boost4asio6detail12posix_threadD2Ev[_ZN5boost4asio6detail12posix_threadD2Ev]+0x26): undefined reference to `pthread_detach'
/usr/bin/ld: /tmp/handler-8f246d.o: in function `boost::asio::ssl::detail::openssl_init_base::do_init::~do_init()':
handler.cpp:(.text._ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev[_ZN5boost4asio3ssl6detail17openssl_init_base7do_initD2Ev]+0x12): undefined reference to `CONF_modules_unload'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `main':
Main.cpp:(.text+0x30): undefined reference to `web::uri::uri(char const*)'
/usr/bin/ld: Main.cpp:(.text+0x6c): undefined reference to `web::http::methods::GET[abi:cxx11]'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `web::http::experimental::listener::http_listener::open()':
Main.cpp:(.text._ZN3web4http12experimental8listener13http_listener4openEv[_ZN3web4http12experimental8listener13http_listener4openEv]+0x32): undefined reference to `web::http::experimental::listener::details::http_listener_impl::open()'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `pplx::task_options::task_options()':
Main.cpp:(.text._ZN4pplx12task_optionsC2Ev[_ZN4pplx12task_optionsC2Ev]+0x27): undefined reference to `pplx::get_ambient_scheduler()'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `web::http::experimental::listener::http_listener::~http_listener()':
Main.cpp:(.text._ZN3web4http12experimental8listener13http_listenerD2Ev[_ZN3web4http12experimental8listener13http_listenerD2Ev]+0x3a): undefined reference to `web::http::experimental::listener::details::http_listener_impl::close()'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `std::unique_ptr<web::http::experimental::listener::details::http_listener_impl, std::default_delete<web::http::experimental::listener::details::http_listener_impl> > utility::details::make_unique<web::http::experimental::listener::details::http_listener_impl, web::uri>(web::uri&&)':
Main.cpp:(.text._ZN7utility7details11make_uniqueIN3web4http12experimental8listener7details18http_listener_implENS2_3uriEEESt10unique_ptrIT_St14default_deleteISA_EEOT0_[_ZN7utility7details11make_uniqueIN3web4http12experimental8listener7details18http_listener_implENS2_3uriEEESt10unique_ptrIT_St14default_deleteISA_EEOT0_]+0x84): undefined reference to `web::http::experimental::listener::details::http_listener_impl::http_listener_impl(web::uri)'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `pplx::details::_CancellationTokenState::_DeregisterCallback(pplx::details::_CancellationTokenRegistration*)':
Main.cpp:(.text._ZN4pplx7details23_CancellationTokenState19_DeregisterCallbackEPNS0_30_CancellationTokenRegistrationE[_ZN4pplx7details23_CancellationTokenState19_DeregisterCallbackEPNS0_30_CancellationTokenRegistrationE]+0x188): undefined reference to `pplx::details::platform::GetCurrentThreadId()'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `pplx::details::_TaskCollectionImpl::_RunTask(void (*)(void*), void*, pplx::details::_TaskInliningMode)':
Main.cpp:(.text._ZN4pplx7details19_TaskCollectionImpl8_RunTaskEPFvPvES2_NS0_17_TaskInliningModeE[_ZN4pplx7details19_TaskCollectionImpl8_RunTaskEPFvPvES2_NS0_17_TaskInliningModeE]+0x38): undefined reference to `pplx::get_ambient_scheduler()'
/usr/bin/ld: /tmp/Main-9a7bc9.o: in function `pplx::details::_CancellationTokenRegistration::_Invoke()':
Main.cpp:(.text._ZN4pplx7details30_CancellationTokenRegistration7_InvokeEv[_ZN4pplx7details30_CancellationTokenRegistration7_InvokeEv]+0x15): undefined reference to `pplx::details::platform::GetCurrentThreadId()'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
the files are as follows: include/handler.h
:文件如下: include/handler.h
:
#ifndef HANDLER_H
#define HANDLER_H
#include <cpprest/http_listener.h>
using namespace web;
using namespace web::http;
void handle_get(http_request request);
#endif
src/handler.cpp
: src/handler.cpp
:
#include <iostream>
#include "../include/handler.h"
using namespace std;
void handle_get(http_request request)
{
cout << "in handler!!!" << endl;
}
main.cpp
#include <iostream>
#include <cpprest/http_listener.h>
#include "include/handler.h"
using namespace web;
using namespace web::http;
using namespace web::http::experimental::listener;
using namespace std;
int main(const int, const char **)
{
http_listener listener("http://*:8080");
listener.support(methods::GET, handle_get);
try
{
listener
.open()
.then([&listener]() { cout << "starting to listen..." << endl; })
.wait();
while (true)
;
}
catch (exception const &e)
{
cout << e.what() << endl;
}
return 0;
}
Any help on resolving this would be much appreciated.任何有关解决此问题的帮助将不胜感激。
handler.cpp: [...] undefined reference to `pthread_join'
Main.cpp: [...] undefined reference to `web::uri::uri(char const*)'
It looks like you aren't linking to the libraries that contain those functions, which is why the linker can't find them.看起来您没有链接到包含这些函数的库,这就是 linker 找不到它们的原因。
To link to the pthread_*() functions, you can add -pthread
to your compile-line arguments.要链接到 pthread_*() 函数,可以将-pthread
添加到编译行 arguments。
For the web::uri::uri(char * const)
function, the argument will probably be something like -lcpprestsdk
, but I'm not sure of the exact library name.对于web::uri::uri(char * const)
function,参数可能类似于-lcpprestsdk
,但我不确定确切的库名称。 Look for a file named libcpprestsdk.so
or libcpprestsdk.a
or similar, and derive the -l
argument from that (eg to link to libxyz.so
or libxyz.a
, you would specify -lxyz
)查找名为libcpprestsdk.so
或libcpprestsdk.a
或类似文件的文件,并从中派生-l
参数(例如,要链接到libxyz.so
或libxyz.a
,您将指定-lxyz
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.