Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link multiple libraries using CMake

Tags:

c++

cmake

I have some code to do with DCMTK. I can successfully build and run it if I use g++ from the command line. This is the code:

#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"

int main()
{
DcmFileFormat fileformat;
OFCondition status = fileformat.loadFile("test.dcm");
if (status.good())
{
   OFString patientsName;
   if (fileformat.getDataset()->findAndGetOFString(DCM_PatientsName, patientsName).good())
   {
      cout << "Patient's Name: " << patientsName << endl;
   } else
     cerr << "Error: cannot access Patient's Name!" << endl;
} else
cerr << "Error: cannot read DICOM file (" << status.text() << ")" << endl;
return 0;
}

This is the build command:

g++ testeapp.cxx -DHAVE_CONFIG_H -I/path_to_dcmtk/include -L/path_to_dcmtk/lib -pthread -ldcmdata -lz -loflog -lofstd -o main

I want to make a CMakeLists.txt to build it in Kdevelop. This is what I currently have:

    # Configure toplevel directories
    SET( PREFIX     ${CMAKE_INSTALL_PREFIX} CACHE PATH "Top level.")
    SET( INCLUDEDIR ${PREFIX}/include       CACHE PATH "Include files.")
    SET( LIBDIR     ${PREFIX}/lib           CACHE PATH "Libraries.")
    FIND_PACKAGE ( Threads REQUIRED )
    # Configure DCMTK
    FIND_PATH( DINIFTI_DCMTK_INCLUDE dcmtk
               PATHS ${INCLUDEDIR}
               PATH_SUFFIXES dcmtk
               DOC "Path to the DCMTK headers." )
    FIND_LIBRARY(DINIFTI_DCMTK_LIB NAMES dcmdata ofstd oflog 
                 HINTS ${LIBDIR} ${LIBDIR})
TARGET_LINK_LIBRARIES( dinifti ${DINIFTI_DCMTK_LIB}
                               ${DINIFTI_ZNZ_LIB}
                               ${CMAKE_THREAD_LIBS_INIT}
                               z )             

But when I build it, it has this error:

/usr/local/lib/libdcmdata.a(dcfilefo.o): In function `DcmFileFormat::remove(DcmItem*)':
dcfilefo.cc:(.text+0x1788): undefined reference to `log4cplus::Logger::forcedLog(int, OFString const&, char const*, int, char const*) const'

Can you help me to fix the error? Thank you.

like image 858
user2039786 Avatar asked Mar 04 '13 17:03

user2039786


People also ask

How do I link libraries in CMakeLists?

Let's start by adding the library's directory as a subdirectory to our myapp project. add_subdirectory makes the library test defined in libtestproject available to the build. In target_link_libraries we tell CMake to link it to our executable. CMake will make sure to first build test before linking it to myapp.

What does target link libraries do in CMake?

target_link_libraries is probably the most useful and confusing command in CMake. It takes a target ( another ) and adds a dependency if a target is given. If no target of that name ( one ) exists, then it adds a link to a library called one on your path (hence the name of the command).


1 Answers

It looks like you expect the find_library call to populate the variable DINIFTI_DCMTK_LIB with 3 separate libraries.

This isn't how find_library works. The different arguments after NAMES represent all the various names a single library could be called. This allows the command to work cross-platform, where the same library could be called different things on different platforms.

A minor issue is that you probably should prefer using PATHS instead of HINTS here. Form the docs:

... the HINTS option ... should be paths computed by system introspection, such as a hint provided by the location of another item already found. Hard-coded guesses should be specified with the PATHS option.

I imagine you want something more like:

find_library(DINIFTI_DCMTK_LIB NAMES dcmdata PATHS ${LIBDIR})
find_library(OFSTD_LIB NAMES ofstd PATHS ${LIBDIR})
find_library(OFLOG_LIB NAMES oflog PATHS ${LIBDIR})
target_link_libraries(dinifti ${DINIFTI_DCMTK_LIB}
                              ${OFLOG_LIB}
                              ${OFSTD_LIB}
                              ${DINIFTI_ZNZ_LIB}
                              ${CMAKE_THREAD_LIBS_INIT}
                              z)
like image 129
Fraser Avatar answered Sep 28 '22 02:09

Fraser