微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 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_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)

文件如下: 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

#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;
}

解决此问题的任何帮助将不胜感激。

解决方法

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.solibcpprestsdk.a 或类似文件的文件,并从中派生出 -l 参数(例如,要链接到 libxyz.solibxyz.a,您可以指定-lxyz)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。