简体   繁体   English

如何使用 CMake 链接多个库

[英]How to link multiple libraries using CMake

I have some code to do with DCMTK.我有一些与 DCMTK 相关的代码。 I can successfully build and run it if I use g++ from the command line.如果我从命令行使用 g++,我可以成功构建并运行它。 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.我想制作一个 CMakeLists.txt 以在 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.谢谢你。

It looks like you expect the find_library call to populate the variable DINIFTI_DCMTK_LIB with 3 separate libraries.看起来您希望find_library调用使用 3 个单独的库填充变量DINIFTI_DCMTK_LIB

This isn't how find_library works.这不是find_library工作方式。 The different arguments after NAMES represent all the various names a single library could be called. NAMES之后的不同参数代表单个库可以调用的所有不同名称。 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.一个小问题是您可能更喜欢在此处使用PATHS而不是HINTS 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. ... HINTS选项 ... 应该是由系统自省计算的路径,例如由已找到的另一个项目的位置提供的提示。 Hard-coded guesses should be specified with the PATHS option.硬编码的猜测应该用PATHS选项指定。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM