Search code examples
c++cmakemingw-w64boost-ublas

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

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

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).

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. The 2 functions in CamData are also of similar type:

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:

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. 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  

Solution

  • Well, here's why I recieved the error while linking and not while compiling.

    The functions in the cpp files were declared like this:

    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.