-1

我的项目正在使用以下内容构建 : Eclipse,,, CMakeLists.txtMinGW 4.8.1

项目编译正常,应用链接正常。但添加后,

'Helper.cpp' 中的 3 个函数和 'CamData.cpp' 中的 2 个函数,类型为 boost::ublas::matrix

链接器放弃了,并且无法再找到函数(未定义的引用错误),尽管它可以编译它们(编译目标文件 *.cpp.obj 时没有错误)。

代码结构

Main.cpp
CMain.cpp
Helper.cpp (Boost::ublas::matrix<double> Fnction1, ...) 
CamData.cpp (Boost::ublas::matrix<double> Funtion4, ...)

Helper 类中创建的函数如下。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  
4

1 回答 1

0

好吧,这就是我在链接时而不是在编译时收到错误的原因。

cpp 文件中的函数声明如下:

Type Function_Name (arguments)
{
  // do something
}   

但它应该是这样的:

Type Class_name::Function_Name (arguments)
{
  // do something
}   

因此它在链接时找不到它们,但可以在cpp文件中单独编译它们。

于 2016-09-12T19:39:05.997 回答