scap3y
scap3y

Reputation: 1198

Linkage error in Project

I have a project which whose structure is defined in the following way (link to project is here):

project_source/
    CMakeLists.txt
    .gitignore
    src/
        main.cpp -> main program file
        windows/  -> contains Unix header implementations for Windows
            CMakeLists.txt
            dirent.h
            getopt.cpp
            getopt.h
        classes/ -> custom class directory
            CMakeLists.txt
            utlities.cpp
            utilities.h
            itk/ -> classes using ITK
                CMakeLists.txt
                basicITK.cpp
                basicITK.h

I am trying to use the function basic::GetImageInfo() which is declared in basicITK.h but the compiler gives a linkage error, saying that "undefined reference to GetImageInfo()" in both GCC and MSVC 2012. I am confused since when I am trying to go to the definition of this function from main.cpp using Visual Studio (by pressing F12), it is able to go the definition perfectly fine (which suggests there aren't any issues with the include). In the same vein, when I invoke basic::fileExists() from utilities.h, it works fine.

I have also tried to have the folder /classes/itk/ directly as an include using CMakeLists but even that doesn't work. But, when I move basicITK class under /classes/, it works perfectly fine. However, I would rather keep a segregated folder structure to simplify future extension.

Any help on this would be highly appreciated! Thanks in advance.

UPDATE:

For ease, I have removed ITK dependency from the project. Now, basic::GetImageInfo() has the same functionality as basic::fileExists().

Upvotes: 0

Views: 72

Answers (2)

scap3y
scap3y

Reputation: 1198

I fixed the issue by making CMake link to /classes/itk/ from the main CMakeLists. If anyone is interested, I will keep the github project page public.

Thanks @SNC for the answer but it wasn't exactly what I was looking for.

Upvotes: 0

SNC
SNC

Reputation: 23

ld gives 'undefined reference' when there is a function declaration without implementation. Function 'GetImageInfo' is declared in 'basicITK.h', but the implementation is located in a static library (or DLL - if you use Windows) which you didn't tell ld to include during linking. You must run ld with an option which tells it to link external libraries to your program (for example, on some Linux OSs this option is '-ldefined library name', so if my program uses zlib for example, then I have to run ld with '-lz' option).

Upvotes: 1

Related Questions