gcc -O3 标志导致 -Winline "call is unlikely and code size would grow" 警告
gcc -O3 flag causes -Winline "call is unlikely and code size would grow" warning
我在 Ubuntu 14.04 和 gcc 4.9.3。我遇到了一个奇怪的问题,当我启用 O3 优化时,会弹出一些 "call is unlikely and code size would grow [-Werror=inline]" 错误。我的代码中没有 inline 关键字。为什么 gcc 内联优化代码到触发它自己的警告的程度?
显然禁用-Winline 可以编译,但是有没有更好的方法来解决这个问题?
我正在使用的库是点云库,为了完整起见,代码如下所示。
我的 CMakeLists
cmake_minimum_required(VERSION 2.8)
project(Test)
set(PROJECT_SRCS
${PROJECT_SOURCE_DIR}/Test.cpp
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Werror -Winline")
# causes error
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
find_package( PCL 1.7 REQUIRED )
include_directories(${PCL_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} ${PROJECT_SRCS})
target_link_libraries(${PROJECT_NAME} ${PCL_LIBRARIES})
Test.cpp
#include <string>
#include <vector>
#include "pcl/common/common_headers.h"
#include "pcl/io/obj_io.h"
#include "pcl/io/ply_io.h"
#include "pcl/io/vtk_lib_io.h"
#include "pcl/visualization/pcl_visualizer.h"
int main(){
pcl::TextureMesh mesh;
pcl::PointCloud<pcl::PointNormal> xyz;
pcl::PointNormal point1;
pcl::PointNormal point2;
pcl::PointNormal point3;
point1.x = 0;
point2.x = 1;
point3.x = 0;
point1.y = 0;
point2.y = 0;
point3.y = 1;
point1.z = 2;
point2.z = 2;
point3.z = 2;
xyz.push_back(point1);
xyz.push_back(point2);
xyz.push_back(point3);
pcl::toPCLPointCloud2(xyz, mesh.cloud);
std::vector<pcl::Vertices> mesh_poly;
std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > mesh_tex;
pcl::TexMaterial mesh_material;
pcl::Vertices v;
v.vertices.push_back(0);
v.vertices.push_back(1);
v.vertices.push_back(2);
mesh_poly.push_back(v);
Eigen::Vector2f tex1;
Eigen::Vector2f tex2;
Eigen::Vector2f tex3;
tex1(0) = -1.0;
tex1(1) = 0.0;
tex2(0) = -1.0;
tex2(1) = 1.0;
tex3(0) = 2.0;
tex3(1) = 0.0;
mesh_tex.push_back(tex1);
mesh_tex.push_back(tex2);
mesh_tex.push_back(tex3);
mesh_material.tex_file = "lena.png";
mesh_material.tex_name = "material_0";
mesh.tex_polygons.push_back(mesh_poly);
mesh.tex_coordinates.push_back(mesh_tex);
mesh.tex_materials.push_back(mesh_material);
pcl::io::saveOBJFile("out.obj", mesh);
return 0;
}
编辑:更新错误消息
In file included from /usr/local/include/pcl-1.8/pcl/io/obj_io.h:40:0,
from /home/david/test/Test.cpp:5:
/usr/local/include/pcl-1.8/pcl/TextureMesh.h: In function ‘int main()’:
/usr/local/include/pcl-1.8/pcl/TextureMesh.h:50:10: error: inlining failed in call to ‘pcl::TexMaterial::~TexMaterial()’: call is unlikely and code size would grow [-Werror=inline]
struct TexMaterial
^
/home/david/test/Test.cpp:37:20: error: called from here [-Werror=inline]
pcl::TexMaterial mesh_material;
^
In file included from /usr/local/include/pcl-1.8/pcl/io/obj_io.h:40:0,
from /home/david/test/Test.cpp:5:
/usr/local/include/pcl-1.8/pcl/TextureMesh.h:91:10: error: inlining failed in call to ‘pcl::TextureMesh::~TextureMesh()’: call is unlikely and code size would grow [-Werror=inline]
struct TextureMesh
^
/home/david/test/Test.cpp:14:20: error: called from here [-Werror=inline]
pcl::TextureMesh mesh;
^
In file included from /usr/local/include/pcl-1.8/pcl/io/obj_io.h:40:0,
from /home/david/test/Test.cpp:5:
/usr/local/include/pcl-1.8/pcl/TextureMesh.h:50:10: error: inlining failed in call to ‘pcl::TexMaterial::~TexMaterial()’: call is unlikely and code size would grow [-Werror=inline]
struct TexMaterial
^
/home/david/test/Test.cpp:37:20: error: called from here [-Werror=inline]
pcl::TexMaterial mesh_material;
^
In file included from /usr/local/include/pcl-1.8/pcl/io/obj_io.h:40:0,
from /home/david/test/Test.cpp:5:
/usr/local/include/pcl-1.8/pcl/TextureMesh.h:91:10: error: inlining failed in call to ‘pcl::TextureMesh::~TextureMesh()’: call is unlikely and code size would grow [-Werror=inline]
struct TextureMesh
^
/home/david/test/Test.cpp:14:20: error: called from here [-Werror=inline]
pcl::TextureMesh mesh;
^
cc1plus: all warnings being treated as errors
make[2]: *** [CMakeFiles/Test.dir/Test.cpp.o] Error 1
make[1]: *** [CMakeFiles/Test.dir/all] Error 2
make: *** [all] Error 2
参见this and also consider this answer。
即使 -O3
优化级别上没有 inline
关键字,GCC 也会打开自动 内联 。因此,正如 NathanOliver 指出的那样,PCL 库中可能不太可能有可调用代码(不幸的是,我对此并不熟悉)。
我会将优化级别降低到 -O2
或使用 fno-inline-functions
.
禁用激进的 内联
classes/structs use 可能有内联成员,即使这在代码中不明确。
例如,pcl::TextureMesh
结构将有一个隐式析构函数,根据 C++ 标准:
An implicitly-declared destructor is an inline public member of its class.
我在 Ubuntu 14.04 和 gcc 4.9.3。我遇到了一个奇怪的问题,当我启用 O3 优化时,会弹出一些 "call is unlikely and code size would grow [-Werror=inline]" 错误。我的代码中没有 inline 关键字。为什么 gcc 内联优化代码到触发它自己的警告的程度?
显然禁用-Winline 可以编译,但是有没有更好的方法来解决这个问题?
我正在使用的库是点云库,为了完整起见,代码如下所示。
我的 CMakeLists
cmake_minimum_required(VERSION 2.8)
project(Test)
set(PROJECT_SRCS
${PROJECT_SOURCE_DIR}/Test.cpp
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -W -Werror -Winline")
# causes error
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
find_package( PCL 1.7 REQUIRED )
include_directories(${PCL_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} ${PROJECT_SRCS})
target_link_libraries(${PROJECT_NAME} ${PCL_LIBRARIES})
Test.cpp
#include <string>
#include <vector>
#include "pcl/common/common_headers.h"
#include "pcl/io/obj_io.h"
#include "pcl/io/ply_io.h"
#include "pcl/io/vtk_lib_io.h"
#include "pcl/visualization/pcl_visualizer.h"
int main(){
pcl::TextureMesh mesh;
pcl::PointCloud<pcl::PointNormal> xyz;
pcl::PointNormal point1;
pcl::PointNormal point2;
pcl::PointNormal point3;
point1.x = 0;
point2.x = 1;
point3.x = 0;
point1.y = 0;
point2.y = 0;
point3.y = 1;
point1.z = 2;
point2.z = 2;
point3.z = 2;
xyz.push_back(point1);
xyz.push_back(point2);
xyz.push_back(point3);
pcl::toPCLPointCloud2(xyz, mesh.cloud);
std::vector<pcl::Vertices> mesh_poly;
std::vector<Eigen::Vector2f, Eigen::aligned_allocator<Eigen::Vector2f> > mesh_tex;
pcl::TexMaterial mesh_material;
pcl::Vertices v;
v.vertices.push_back(0);
v.vertices.push_back(1);
v.vertices.push_back(2);
mesh_poly.push_back(v);
Eigen::Vector2f tex1;
Eigen::Vector2f tex2;
Eigen::Vector2f tex3;
tex1(0) = -1.0;
tex1(1) = 0.0;
tex2(0) = -1.0;
tex2(1) = 1.0;
tex3(0) = 2.0;
tex3(1) = 0.0;
mesh_tex.push_back(tex1);
mesh_tex.push_back(tex2);
mesh_tex.push_back(tex3);
mesh_material.tex_file = "lena.png";
mesh_material.tex_name = "material_0";
mesh.tex_polygons.push_back(mesh_poly);
mesh.tex_coordinates.push_back(mesh_tex);
mesh.tex_materials.push_back(mesh_material);
pcl::io::saveOBJFile("out.obj", mesh);
return 0;
}
编辑:更新错误消息
In file included from /usr/local/include/pcl-1.8/pcl/io/obj_io.h:40:0,
from /home/david/test/Test.cpp:5:
/usr/local/include/pcl-1.8/pcl/TextureMesh.h: In function ‘int main()’:
/usr/local/include/pcl-1.8/pcl/TextureMesh.h:50:10: error: inlining failed in call to ‘pcl::TexMaterial::~TexMaterial()’: call is unlikely and code size would grow [-Werror=inline]
struct TexMaterial
^
/home/david/test/Test.cpp:37:20: error: called from here [-Werror=inline]
pcl::TexMaterial mesh_material;
^
In file included from /usr/local/include/pcl-1.8/pcl/io/obj_io.h:40:0,
from /home/david/test/Test.cpp:5:
/usr/local/include/pcl-1.8/pcl/TextureMesh.h:91:10: error: inlining failed in call to ‘pcl::TextureMesh::~TextureMesh()’: call is unlikely and code size would grow [-Werror=inline]
struct TextureMesh
^
/home/david/test/Test.cpp:14:20: error: called from here [-Werror=inline]
pcl::TextureMesh mesh;
^
In file included from /usr/local/include/pcl-1.8/pcl/io/obj_io.h:40:0,
from /home/david/test/Test.cpp:5:
/usr/local/include/pcl-1.8/pcl/TextureMesh.h:50:10: error: inlining failed in call to ‘pcl::TexMaterial::~TexMaterial()’: call is unlikely and code size would grow [-Werror=inline]
struct TexMaterial
^
/home/david/test/Test.cpp:37:20: error: called from here [-Werror=inline]
pcl::TexMaterial mesh_material;
^
In file included from /usr/local/include/pcl-1.8/pcl/io/obj_io.h:40:0,
from /home/david/test/Test.cpp:5:
/usr/local/include/pcl-1.8/pcl/TextureMesh.h:91:10: error: inlining failed in call to ‘pcl::TextureMesh::~TextureMesh()’: call is unlikely and code size would grow [-Werror=inline]
struct TextureMesh
^
/home/david/test/Test.cpp:14:20: error: called from here [-Werror=inline]
pcl::TextureMesh mesh;
^
cc1plus: all warnings being treated as errors
make[2]: *** [CMakeFiles/Test.dir/Test.cpp.o] Error 1
make[1]: *** [CMakeFiles/Test.dir/all] Error 2
make: *** [all] Error 2
参见this and also consider this answer。
即使 -O3
优化级别上没有 inline
关键字,GCC 也会打开自动 内联 。因此,正如 NathanOliver 指出的那样,PCL 库中可能不太可能有可调用代码(不幸的是,我对此并不熟悉)。
我会将优化级别降低到 -O2
或使用 fno-inline-functions
.
classes/structs use 可能有内联成员,即使这在代码中不明确。
例如,pcl::TextureMesh
结构将有一个隐式析构函数,根据 C++ 标准:
An implicitly-declared destructor is an inline public member of its class.