为什么在向 class 添加新功能后,应用程序编译但在链接阶段失败?
Why does the application compile but fails at linking stage, after adding new functions to a class?
我的项目正在使用以下内容构建:
Eclipse
,
CMakeLists.txt
,
MinGW 4.8.1
项目正常编译和应用链接。但是添加后,
3 Functions in 'Helper.cpp' and 2 Functions in 'CamData.cpp' , of the type boost::ublas::matrix
链接器放弃,无法再找到函数(未定义引用错误)虽然它可以编译它们(编译目标文件时没有错误*.cpp.obj).
代码结构
Main.cpp
CMain.cpp
Helper.cpp (Boost::ublas::matrix<double> Fnction1, ...)
CamData.cpp (Boost::ublas::matrix<double> Funtion4, ...)
Helperclass中创建的函数如下。 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);
};
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})
注释掉 main.cpp 中的函数和它们的调用后,程序可以再次链接。关于可能导致此问题的任何想法?
下面的错误日志
[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
好吧,这就是为什么我在链接时收到错误而不是在编译时收到错误的原因。
cpp 文件中的函数声明如下:
Type Function_Name (arguments)
{
// do something
}
但应该是这样的:
Type Class_name::Function_Name (arguments)
{
// do something
}
因此在链接时找不到它们,但可以在 cpp
文件中单独编译它们。
我的项目正在使用以下内容构建:
Eclipse
,
CMakeLists.txt
,
MinGW 4.8.1
项目正常编译和应用链接。但是添加后,
3 Functions in 'Helper.cpp' and 2 Functions in 'CamData.cpp' , of the type boost::ublas::matrix
链接器放弃,无法再找到函数(未定义引用错误)虽然它可以编译它们(编译目标文件时没有错误*.cpp.obj).
代码结构
Main.cpp
CMain.cpp
Helper.cpp (Boost::ublas::matrix<double> Fnction1, ...)
CamData.cpp (Boost::ublas::matrix<double> Funtion4, ...)
Helperclass中创建的函数如下。 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);
};
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})
注释掉 main.cpp 中的函数和它们的调用后,程序可以再次链接。关于可能导致此问题的任何想法?
下面的错误日志
[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
好吧,这就是为什么我在链接时收到错误而不是在编译时收到错误的原因。
cpp 文件中的函数声明如下:
Type Function_Name (arguments)
{
// do something
}
但应该是这样的:
Type Class_name::Function_Name (arguments)
{
// do something
}
因此在链接时找不到它们,但可以在 cpp
文件中单独编译它们。