4

I am on Ubuntu 14.04 and gcc 4.9.3. I am encountering a weird problem where when I enable O3 optimization there are a few "call is unlikely and code size would grow [-Werror=inline]" error popping up. There is no inline keyword in my code. Why would gcc inline optimize the code to the point that it triggers it's own warnings?

Obviously disabling -Winline will compile but is there a better way to solve this problem?

The library I am working with is the Point Cloud Library, for completeness the code is shown below.

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

EDIT: update with error message

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
user3667089
  • 2,996
  • 5
  • 30
  • 56

2 Answers2

3

See this and also consider this answer.

GCC turns on automatic inlining even if there is no inline keyword on -O3 optimization level. So, as NathanOliver put out, there may be unlikely callable code in the PCL library (unfortunately, I'm not familiar with it). I would reduce the optimization level to -O2 or disable aggressive inlining with fno-inline-functions.

Community
  • 1
  • 1
Duarte Patrício
  • 133
  • 1
  • 10
  • I still get the same error with `-O2` and `fno-inline-functions`. Do you think this is more of a PCL problem or gcc problem? I am more inclined to it's a gcc problem because it shouldn't optimize the code to the point that triggers it's own warnings – user3667089 Apr 28 '16 at 18:04
  • I think it is a PCL problem, mainly because of what [Michael Burr](http://stackoverflow.com/a/36922349/2315604) just pointed out. You still have the `-Winline` flag, don't you? Remove it since it is always warning whenever an *inlining* fails (and it will pass that warning to the *warnings are errors* flag). – Duarte Patrício Apr 28 '16 at 18:12
3

The classes/structs use may have inline members, even if that's not explicit in the code.

For example, the pcl::TextureMesh struct will have an implicit destructor, which according to the C++ standard:

An implicitly-declared destructor is an inline public member of its class.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • I went into the source code of PCL and add a empty destructor and the error is gone! Does that means we should always explicitly declare a destructor to avoid this kind of error? – user3667089 Apr 28 '16 at 18:35
  • 1
    No. I would say that `-Winline` should be used for informational/troubleshooting purposes only and should probably not be used with `-Werror`. – Michael Burr Apr 28 '16 at 18:44
  • 4
    Note: I think you can force `Winline` to remain only a warning even with `-Werror` by adding the following option (it probably has to come after the `-Werror` option on the command line): `-Wno-error=inline` – Michael Burr Apr 28 '16 at 19:01