[英]Why does the application compile but fails at linking stage, after adding new functions to a class?
My project is being built using the following: Eclipse
, CMakeLists.txt
, MinGW 4.8.1
我的项目正在使用以下内容构建:
Eclipse
、 CMakeLists.txt
、 MinGW 4.8.1
The project compiles and application links normally.项目编译正常,应用链接正常。 But after adding,
但是添加后,
3 Functions in 'Helper.cpp' and 2 Functions in 'CamData.cpp' , of the type boost::ublas::matrix
'Helper.cpp' 中的 3 个函数和 'CamData.cpp' 中的 2 个函数,类型为 boost::ublas::matrix
The linker gives up, and can not find the functions anymore (undefined reference Error) though it can compile them ( no error while compiling the object files *.cpp.obj ).链接器放弃了,并且无法再找到这些函数(未定义的引用错误),尽管它可以编译它们(编译目标文件 *.cpp.obj 时没有错误)。
Code Structure代码结构
Main.cpp
CMain.cpp
Helper.cpp (Boost::ublas::matrix<double> Fnction1, ...)
CamData.cpp (Boost::ublas::matrix<double> Funtion4, ...)
The functions in Helper class that were created are as follows.创建的 Helper 类中的函数如下。 The 2 functions in CamData are also of similar type:
CamData 中的 2 个函数也是类似的类型:
class Helper{
Helper();
virtual ~Helper();
template<typename T>
using bMatrix = boost::numeric::ublas::matrix<T>;
bMatrix<double> getmatrixQ(double w, double x, double y, double z);
bMatrix<double> rotate_x(bMatrix<double> M, double angleinrad);
bMatrix<double> getTransformationMatrix(bMatrix<double> M, double x, double y, double z);
};
The relevant parts of the cmakelist.txt are as follows: cmakelist.txt的相关部分如下:
FIND_PACKAGE (Boost REQUIRED COMPONENTS date_time filesystem system)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../src)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../src/InputParams)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/../src/Utilities)
SET(CMAKE_CXX_FLAGS "${warnings}"
CACHE STRING "Flags used by the compiler during all build types" FORCE)
SET(CMAKE_CXX_FLAGS "-Wall -std=c++11")
ADD_EXECUTABLE(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/../src/main.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/Utilities/CHelper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/InputParams/CCamData.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/CMain.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/COutput.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../src/CThreads.cpp)
TARGET_LINK_LIBRARIES (${PROJECT_NAME} ${Boost_LIBRARIES})
After commenting out the functions and thier calls in main.cpp the program can be linked again.注释掉 main.cpp 中的函数和它们的调用后,程序可以再次链接。 Any Ideas on what could be causing this?
关于可能导致这种情况的任何想法?
Error Log Below下面的错误日志
[100%] Linking CXX executable Project.exe
CMakeFiles\Project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x116c): undefined reference to `esg::CHelper::getmatrixQ(double, doubl
e, double, double)'
CMakeFiles\Project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1230): undefined reference to `esg::CHelper::rotate_x(boost::numeric:
:ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned int, int>, boost::numeric::ublas::unbounded_array<double, std::alloca
tor<double> > >, double)'
CMakeFiles\Project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1344): undefined reference to `esg::CHelper::getTransformationMatrix(
boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned int, int>, boost::numeric::ublas::unbounded_array<doub
le, std::allocator<double> > >, double, double, double)'
CMakeFiles\Project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x149d): undefined reference to `esg::CCamData::initpinhole(esg::Pinhol
eIntrinsics&)'
CMakeFiles\Project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x15d3): undefined reference to `esg::CCamData::createProjectionMatrix(
boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned int, int>, boost::numeric::ublas::unbounded_array<doub
le, std::allocator<double> > >, boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<unsigned int, int>, boost::nume
ric::ublas::unbounded_array<double, std::allocator<double> > >&, long)'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\Project.dir\build.make:548: recipe for target 'Project.exe' failed
mingw32-make.exe[2]: *** [Project.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Project.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/Project.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make.exe: *** [all] Error 2
Well, here's why I recieved the error while linking and not while compiling.好吧,这就是为什么我在链接时而不是在编译时收到错误。
The functions in the cpp files were declared like this: cpp 文件中的函数声明如下:
Type Function_Name (arguments)
{
// do something
}
but it should have been like this:但它应该是这样的:
Type Class_name::Function_Name (arguments)
{
// do something
}
Hence it could not find them while linking but could compile them individually in the cpp
file.因此,它在链接时找不到它们,但可以在
cpp
文件中单独编译它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.