0

我想在 C++ 中使用nghtt2_asio编译一个简单的服务器应用程序。

它在共享库下完美编译:

g++ server.cpp -O2 -std=c++20 -Wall -Wextra -lnghttp2_asio -lboost_system -lssl -lcrypto -o server

但是当我静态编译它时:

g++ server.cpp -static -O2 -std=c++20 -Wall -Wextra -o server -lssl -ldl -pthread -lcrypto -ldl -pthread -lnghttp2_asio -lboost_system

链接器给了我这些错误:

/usr/bin/ld: /usr/local/lib/libcrypto.a(dso_dlfcn.o): in function `dlfcn_globallookup':
(.text+0x14): attenzione: Using 'dlopen' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: /usr/local/lib/libcrypto.a(b_addr.o): in function `BIO_lookup_ex':
(.text+0xd0b): attenzione: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: /usr/local/lib/libcrypto.a(b_sock.o): in function `BIO_gethostbyname':
(.text+0x72): attenzione: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_common.o): in function `nghttp2::asio_http2::nghttp2_category_impl::message[abi:cxx11](int) const':
/home/user/Downloads/nghttp2/src/asio_common.cc:40: undefined reference to `nghttp2_strerror'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server.o): in function `int nghttp2::asio_http2::server::http2_handler::on_read<8192ul>(boost::array<unsigned char, 8192ul> const&, unsigned long)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.h:103: undefined reference to `nghttp2_session_mem_recv'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server.o): in function `boost::asio::ssl::detail::engine::engine(ssl_ctx_st*)':
/usr/include/boost/asio/ssl/detail/impl/engine.ipp:55: undefined reference to `BIO_new_bio_pair'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server.o): in function `int nghttp2::asio_http2::server::http2_handler::on_write<65536ul>(boost::array<unsigned char, 65536ul>&, unsigned long&)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.h:129: undefined reference to `nghttp2_session_mem_send'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server.o): in function `int nghttp2::asio_http2::server::http2_handler::on_read<8192ul>(boost::array<unsigned char, 8192ul> const&, unsigned long)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.h:103: undefined reference to `nghttp2_session_mem_recv'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server.o): in function `int nghttp2::asio_http2::server::http2_handler::on_write<65536ul>(boost::array<unsigned char, 65536ul>&, unsigned long&)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.h:129: undefined reference to `nghttp2_session_mem_send'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server.o): in function `int nghttp2::asio_http2::server::http2_handler::on_read<8192ul>(boost::array<unsigned char, 8192ul> const&, unsigned long)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.h:103: undefined reference to `nghttp2_session_mem_recv'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `nghttp2::asio_http2::server::(anonymous namespace)::on_frame_not_send_callback(nghttp2_session*, nghttp2_frame const*, int, void*)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:230: undefined reference to `nghttp2_submit_rst_stream'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `nghttp2::asio_http2::server::http2_handler::~http2_handler()':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:258: undefined reference to `nghttp2_session_del'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `nghttp2::asio_http2::server::http2_handler::start()':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:274: undefined reference to `nghttp2_session_callbacks_new'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `std::_Bind<void (*(nghttp2_session_callbacks*))(nghttp2_session_callbacks*)>::_Bind(std::_Bind<void (*(nghttp2_session_callbacks*))(nghttp2_session_callbacks*)>&&)':
/usr/include/c++/11.1.0/functional:494: undefined reference to `nghttp2_session_callbacks_del'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `nghttp2::asio_http2::server::http2_handler::start()':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:281: undefined reference to `nghttp2_session_callbacks_set_on_begin_headers_callback'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:283: undefined reference to `nghttp2_session_callbacks_set_on_header_callback'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:285: undefined reference to `nghttp2_session_callbacks_set_on_frame_recv_callback'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:287: undefined reference to `nghttp2_session_callbacks_set_on_data_chunk_recv_callback'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:289: undefined reference to `nghttp2_session_callbacks_set_on_stream_close_callback'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:291: undefined reference to `nghttp2_session_callbacks_set_on_frame_send_callback'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:293: undefined reference to `nghttp2_session_callbacks_set_on_frame_not_send_callback'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:296: undefined reference to `nghttp2_session_server_new'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:302: undefined reference to `nghttp2_submit_settings'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `nghttp2::asio_http2::server::http2_handler::should_stop() const':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:333: undefined reference to `nghttp2_session_want_read'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:334: undefined reference to `nghttp2_session_want_write'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `nghttp2::asio_http2::server::http2_handler::stream_error(int, unsigned int)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:47: undefined reference to `nghttp2_submit_rst_stream'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `nghttp2::asio_http2::server::http2_handler::resume(nghttp2::asio_http2::server::stream&)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:428: undefined reference to `nghttp2_session_resume_data'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `nghttp2::asio_http2::server::(anonymous namespace)::on_header_callback(nghttp2_session*, nghttp2_frame const*, unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned char, void*)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:109: undefined reference to `nghttp2_submit_rst_stream'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `nghttp2::asio_http2::server::http2_handler::submit_trailer(nghttp2::asio_http2::server::stream&, std::multimap<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, nghttp2::asio_http2::header_value, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, nghttp2::asio_http2::header_value> > >)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:387: undefined reference to `nghttp2_submit_trailer'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `nghttp2::asio_http2::server::http2_handler::start_response(nghttp2::asio_http2::server::stream&)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:366: undefined reference to `nghttp2_submit_response'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-asio_server_http2_handler.o): in function `nghttp2::asio_http2::server::http2_handler::push_promise(boost::system::error_code&, nghttp2::asio_http2::server::stream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::multimap<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, nghttp2::asio_http2::header_value, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, nghttp2::asio_http2::header_value> > >)':
/home/user/Downloads/nghttp2/src/asio_server_http2_handler.cc:454: undefined reference to `nghttp2_submit_push_promise'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-http2.o): in function `nghttp2::http2::determine_window_update_transmission(nghttp2_session*, int)':
/home/user/Downloads/nghttp2/src/http2.cc:577: undefined reference to `nghttp2_session_get_effective_recv_data_length'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/http2.cc:578: undefined reference to `nghttp2_session_get_effective_local_window_size'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/http2.cc:580: undefined reference to `nghttp2_session_get_stream_effective_recv_data_length'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/http2.cc:582: undefined reference to `nghttp2_session_get_stream_effective_local_window_size'
/usr/bin/ld: /usr/local/lib/libnghttp2_asio.a(libnghttp2_asio_la-http2.o): in function `nghttp2::http2::check_nv(unsigned char const*, unsigned long, unsigned char const*, unsigned long)':
/home/user/Downloads/nghttp2/src/http2.cc:698: undefined reference to `nghttp2_check_header_name'
/usr/bin/ld: /home/user/Downloads/nghttp2/src/http2.cc:701: undefined reference to `nghttp2_check_header_value'
collect2: error: ld returned 1 exit status

有人可以解释一下会发生什么吗?

4

1 回答 1

1

有人可以解释一下会发生什么吗?

这里有几个问题。

  1. 您有未解析的符号。正如 nm 在评论中所说,您需要添加-libnghttp2.

  2. 您在链接行上的库顺序错误。低级库应该在链接行上跟随高级库。正确的顺序是这样的:

-lnghttp2_asio -libnghttp2 -lboost_system -lssl -lcrypto -ldl

有关说明,请参阅此博客文章。

  1. 您正在链接一个完全静态的二进制文件。

与流行的看法相反,这样的二进制文件(至少在 Linux 上)的可移植性明显低于动态链接的二进制文件。

警告告诉您的是,如果您尝试在安装了不同版本的 GLIBC 的系统上运行此二进制文件(可能是与您构建的系统不同的系统,甚至是相同的系统,但在升级到不同版本的 GLIBC),那么您的二进制文件可能会崩溃(并且很可能会崩溃)。

你最好不要做一个完全静态的链接

注意:您不必libnghttp2_asio动态链接 etc.,只需针对libcandlibdllibcrypto.

于 2021-07-14T04:50:51.130 回答