c – 如何使用CMake链接多个库

前端之家收集整理的这篇文章主要介绍了c – 如何使用CMake链接多个库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些与DCMTK有关的代码.如果我从命令行使用g,我可以成功构建并运行它.这是代码: @H_502_2@#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; }

这是构建命令:

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

我想制作一个CMakeLists.txt来在Kdevelop中构建它.这就是我目前拥有的:

@H_502_2@# 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 )

但是当我构建它时,它有这个错误

@H_502_2@/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'

你能帮我修一下这个错误吗?谢谢.

解决方法

看起来您希望 find_library调用使用3个单独的库填充变量DINIFTI_DCMTK_LIB.

这不是find_library的工作原理. NAMES之后的不同参数表示可以调用单个库的所有各种名称.这允许命令跨平台工作,其中相同的库可以在不同平台上被称为不同的东西.

一个小问题是你可能更喜欢在这里使用PATHS而不是HINTS.形成文档:

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

我想你想要更像的东西:

@H_502_2@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)
原文链接:https://www.f2er.com/c/116684.html

猜你在找的C&C++相关文章