C++ 如何解决c++;用于简单的多文件编译
我尝试试用cpprestsdk库,发现在使用C++ 如何解决c++;用于简单的多文件编译,c++,C++,我尝试试用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_category7
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)
src/handler.cpp
:
#include <iostream>
#include "../include/handler.h"
using namespace std;
void handle_get(http_request request)
{
cout << "in handler!!!" << endl;
}
如果您能帮助解决此问题,我们将不胜感激
handler.cpp: [...] undefined reference to `pthread_join'
Main.cpp: [...] undefined reference to `web::uri::uri(char const*)'
看起来您没有链接到包含这些函数的库,这就是链接器找不到它们的原因
要链接到pthread_*()函数,可以将-pthread
添加到编译行参数中
对于web::uri::uri(char*const)
函数,参数可能类似于-lcpprestsdk
,但我不确定库的确切名称。查找名为libcpprestsdk.so
或libcpprestsdk.a
或类似文件,并从中派生-l
参数(例如,要链接到libxyz.so
或libxyz.a
,您可以指定-lxyz
)
#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;
}
handler.cpp: [...] undefined reference to `pthread_join'
Main.cpp: [...] undefined reference to `web::uri::uri(char const*)'