【问题标题】:Compiling .cu vs .cpp: Compiler errors even without any CUDA code编译 .cu 与 .cpp:即使没有任何 CUDA 代码,也会出现编译器错误
【发布时间】:2019-04-08 13:23:46
【问题描述】:

我编译如下代码:

#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 文件有问题。这是一个已知问题吗?

【问题讨论】:

    标签: c++ cuda nvcc


    【解决方案1】:

    为什么?我以为 nvcc 只是将 CPU 代码转发给主机编译器。

    这对于理解这里发生的事情来说过于简单化了。

    正如您所发现的,nvcc 的行为(默认情况下)因文件扩展名是 .cpp 还是 .cu 而不同,即使对于纯主机代码也是如此

    the nvcc manual 中给出了与.cu 文件(甚至是主机代码)相关的nvcc 预处理的更完整描述。通过运行带有--verbose 开关的nvcc 命令可以获得更完整的描述;您可以通过这种方式全面检查流程。

    简而言之,.cu 文件中的宿主代码在实际到达宿主 C++ 编译器之前要经过几个预处理步骤。 .cpp 文件中的宿主代码在到达宿主 C++ 编译器之前经过的步骤更少。

    此外,当 boost 检测到 nvcc 编译器添加到(甚至)主机代码中的某些宏时,当该主机代码位于 .cu 文件中时,它会采取特定的行为。由 boost 代码检测到的这些宏会导致 boost 的“行为”不同。 Here 就是一个例子。

    我认为the conventional way 中使用的.ipp 文件没有任何问题。这些只是头文件的“附加组件”,遵循标准的包含机制。您的错误输出中指出有很多 .ipp 文件的事实与此无关。这只是错误发生的位置以及 boost 如何对其标头代码进行分区的结果。

    如果您真的想在这里解决这个特定问题,通常的建议是:

    1. 将您的 boost 代码放入 .cpp 文件中 (或)
    2. 使用最新版本的 boost 和 CUDA (和)
    3. 在 developer.nvidia.com 上将观察到的问题报告为(或两者)boost issues 或针对 CUDA 的错误(错误提交描述为 here

    【讨论】:

      猜你喜欢
      • 2017-10-03
      • 2023-03-23
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      相关资源
      最近更新 更多