简体   繁体   English

为什么在向类添加新函数后,应用程序编译但在链接阶段失败?

[英]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我的项目正在使用以下内容构建: EclipseCMakeLists.txtMinGW 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.

相关问题 链接阶段在 Ubuntu 上失败,但在 MacOS 上没有 - Linking stage fails on Ubuntu but not MacOS 为什么在向 class 添加新成员变量后需要重新构建? - Why is it necessary to do a rebuild after adding a new member variable to a class? 与函数相比,为什么在链接阶段没有类重新定义错误? - why there's no class redefinition error in link stage, compared to functions? 添加新对话框后,MFC应用程序启动失败,并显示“ DLL初始化失败” - MFC application launch fails with “DLL Initialization Failed” after adding a new dialog 在QT中添加新的源文件夹后的链接问题 - Linking issues after adding a new source folder in QT 在Visual Studio 2013中,Boost Python类导出无法使用链接错误进行编译 - Boost Python class export fails to compile with linking error in visual studio 2013 如果类是不完整的类型,为什么要编译? - Why does this compile if the class is incomplete type? new()是否也为类的函数分配内存? - Does new() allocate memory for the functions of a class also? 在编译时链接到动态可用的函数 - Linking to dynamically available functions at compile time 为什么将对象引用参数传递给线程函数无法编译? - Why does passing object reference arguments to thread function fails to compile?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM