我编译以下代码:
#include <iostream>
#include <boost/beast.hpp>
int main()
{
std::cout << "Hello, world!\n";
}
通过
$ nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Nov__3_21:07:56_CDT_2017
Cuda compilation tools, release 9.1, V9.1.85
$ nvcc -O0 --std=c++14 -g -Xcompiler=-Wfatal-errors -I./include -I../boost main.cu -o runserver
我收到编译器错误:
boost/beast/core/impl/error.ipp(20): warning: overloaded virtual function "boost::system::error_category::message" is only partially overridden in class "boost::beast::detail::error_codes"
beast/core/impl/error.ipp(55): warning: overloaded virtual function "boost::system::error_category::message" is only partially overridden in class "boost::beast::detail::error_conditions"
boost/beast/core/impl/error.ipp(84): warning: invalid narrowing conversion from "unsigned int" to "int"
boost/beast/core/impl/error.ipp(92): warning: invalid narrowing conversion from "unsigned int" to "int"
boost/asio/impl/error.ipp(32): warning: overloaded virtual function "boost::system::error_category::message" is only partially overridden in class "boost::asio::error::detail::netdb_category"
boost/asio/impl/error.ipp(64): warning: overloaded virtual function "boost::system::error_category::message" is only partially overridden in class "boost::asio::error::detail::addrinfo_category"
boost/asio/impl/error.ipp(94): warning: overloaded virtual function "boost::system::error_category::message" is only partially overridden in class "boost::asio::error::detail::misc_category"
boost/beast/http/impl/error.ipp(21): warning: overloaded virtual function "boost::system::error_category::message" is only partially overridden in class "boost::beast::http::detail::http_error_category"
boost/beast/http/impl/error.ipp(96): warning: invalid narrowing conversion from "unsigned int" to "int"
boost/beast/websocket/impl/error.ipp(20): warning: overloaded virtual function "boost::system::error_category::message" is only partially overridden in class "boost::beast::websocket::detail::error_codes"
boost/beast/websocket/impl/error.ipp(117): warning: overloaded virtual function "boost::system::error_category::message" is only partially overridden in class "boost::beast::websocket::detail::error_conditions"
boost/beast/websocket/impl/error.ipp(144): warning: invalid narrowing conversion from "unsigned int" to "int"
boost/beast/websocket/impl/error.ipp(152): warning: invalid narrowing conversion from "unsigned int" to "int"
boost/beast/zlib/impl/error.ipp(49): warning: overloaded virtual function "boost::system::error_category::message" is only partially overridden in class "boost::beast::zlib::detail::error_codes"
boost/beast/zlib/impl/error.ipp(115): warning: invalid narrowing conversion from "unsigned int" to "int"
boost/beast/http/message.hpp(265): error: static assertion failed with "Fields type requirements not met"
detected during:
instantiation of class "boost::beast::http::header<false, Fields> [with Fields=boost::beast::http::fields]"
(495): here
instantiation of class "boost::beast::http::message<isRequest, Body, Fields> [with isRequest=false, Body=boost::beast::http::string_body, Fields=boost::beast::http::fields]"
boost/beast/websocket/detail/impl_base.hpp(199): here
boost/beast/http/message.hpp(61): error: static assertion failed with "Fields type requirements not met"
detected during:
instantiation of class "boost::beast::http::header<true, Fields> [with Fields=boost::beast::http::fields]"
(495): here
instantiation of class "boost::beast::http::message<isRequest, Body, Fields> [with isRequest=true, Body=boost::beast::http::empty_body, Fields=boost::beast::http::fields]"
boost/beast/websocket/detail/impl_base.hpp(257): here
2 errors detected in the compilation of "/tmp/tmpxft_00002a42_00000000-6_main.cpp1.ii".
但是,如果我将文件后缀更改为.cpp
并使用nvcc进行编译,则不会遇到任何问题,一切都很好。
为什么?我以为nvcc只是将CPU代码转发给主机编译器。
此外,似乎nvcc与.ipp
文件有问题。这是一个已知的问题吗?
为什么?我以为nvcc只是将CPU代码转发给主机编译器。
这对于理解这里发生的事情来说太简单了。
正如您所发现的,nvcc
行为(默认情况下)根据文件扩展名是.cpp
还是.cu
而有所不同,即使对于纯粹的主机代码也是如此。
.cu
中给出了与the nvcc manual文件(甚至主机代码)相关的nvcc预处理的更完整描述。使用nvcc
开关运行--verbose
命令可以获得更完整的描述;您可以通过这种方式详细检查过程。
简而言之,.cu
文件中的主机代码在实际到达主机C ++编译器之前会经历几个预处理步骤。 .cpp
文件中的主机代码在到达主机C ++编译器之前会经历较少的步骤。
此外,当检测到nvcc编译器添加到(甚至)主机代码的某些宏时,当主机代码位于.cu
文件中时,boost会采取特定的行为。通过升压代码检测到的这些宏导致升压“行为”不同。 Here就是一个例子。
我认为在.ipp
中使用的the conventional way文件没有任何问题。根据标准包含机制,这些只是头文件的“附加组件”。您的错误输出中指示了大量.ipp
文件的事实与此无关。这只是错误发生的位置以及boost如何对其标头代码进行分区的结果。
如果你真的想在这里解决这个问题,通常的建议是: