问题:C++ cmake编译 multiple definition of `constructTransformationMatrix(float const&, float const&, float const&, float const&, float const&, float const&)’
问题描述
我在调用xxx.h文件后,cmake编译出现:
CMakeFiles/PaintBot.dir/src/units/Camera.cpp.o: In function std::vector<pcl::PointXYZI, Eigen::aligned_allocator<pcl::PointXYZI> >::reserve(unsigned long)': /home/cecilma693/Documents/paintpot_mqh/include/units/registration.h:13: multiple definition of
constructTransformationMatrix(float const&, float const&, float const&, float const&, float const&, float const&)’
CMakeFiles/PaintBot.dir/src/3dProc/Pcprocess.cpp.o:/home/cecilma693/Documents/paintpot_mqh/include/units/registration.h:13: first defined here
CMakeFiles/PaintBot.dir/src/units/Camera.cpp.o: In function refineAlignment(std::shared_ptr<pcl::PointCloud<pcl::PointXYZI> > const&, std::shared_ptr<pcl::PointCloud<pcl::PointXYZI> > const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, float, float, float, float)': /home/cecilma693/Documents/paintpot_mqh/include/units/registration.h:37: multiple definition of
refineAlignment(std::shared_ptr<pcl::PointCloudpcl::PointXYZI > const&, std::shared_ptr<pcl::PointCloudpcl::PointXYZI > const&, Eigen::Matrix<float, 4, 4, 0, 4, 4> const&, float, float, float, float)’
CMakeFiles/PaintBot.dir/src/3dProc/Pcprocess.cpp.o:/home/cecilma693/Documents/paintpot_mqh/include/units/registration.h:37: first defined here
CMakeFiles/PaintBot.dir/src/3dProc/Geometry.cpp.o: In function BufferedReadWriteFile::BufferedReadWriteFile(char*, int)': /home/cecilma693/Documents/paintpot_mqh/src/3dProc/Geometry.cpp:14: warning: the use of
tempnam’ is dangerous, better use `mkstemp’
collect2: error: ld returned 1 exit status
CMakeFiles/PaintBot.dir/build.make:1663: recipe for target ‘PaintBot’ failed
make[2]: *** [PaintBot] Error 1
CMakeFiles/Makefile2:67: recipe for target ‘CMakeFiles/PaintBot.dir/all’ failed
make[1]: *** [CMakeFiles/PaintBot.dir/all] Error 2
Makefile:83: recipe for target ‘all’ failed
make: *** [all] Error 2
经过一番寻找,再结合这上面描述的问题,发现是编译过程重复引用了不同的xxx.h文件,并且由于编译前后顺序的问题,这个报错的地方是.h文件的第二次编译,因此会出现重定义问题。
解决方法
按照提示,干掉Camera.cpp中的“#include<xxx.h>”,再次编译,通过!有问题请留言交流!