CMAKE链接外部c库

发布于 2024-10-14 05:08:14 字数 1676 浏览 4 评论 0原文

您好,基本上我正在尝试使用 此处 中的 svm。它是用 C 编写的,并给出了如何在 C++ 中使用它的说明:

  • 将“svm_learn.c”、“svm_common.c”和“svm_hideo.c”编译为
    C 代码。
  • 要调用 svm_learn/8 和classify_example/2 的 C++ 程序 (或分类示例线性/2)来自 需要包括以下内容 标题:

    外部“C”{ # 包含“svm_common.h” # 包含“svm_learn.h” }

  • 将“svm_learn.o”、“svm_common.o”和“svm_hideo.o”链接到您的程序。

所以我编译了上述文件并获得了所需的 .o 文件。 比我补充的:

SET( svm_lib_light_obj
    E:\framework\svm_light\build\svm_learn.o
    E:\framework\svm_light\build\svm_common.o
    E:\framework\svm_light\build\svm_hideo.o
)

ADD_LIBRARY(
    svm_lib_light
    STATIC
    EXCLUDE_FROM_ALL
    ${svm_lib_light_obj}
)

SET_SOURCE_FILES_PROPERTIES(
  ${svm_lib_light_obj}
  PROPERTIES
  EXTERNAL_OBJECT true # to say that "this is actually an object file, so it should not be compiled, only linked"
  GENERATED true       # to say that "it is OK that the obj-files do not exist before build time"
  )

SET_TARGET_PROPERTIES(
  svm_lib_light
  PROPERTIES
  LINKER_LANGUAGE C # Or else we get an error message, because cmake can't figure out from the ".o"-suffix that it is a C-linker we need.
  ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib
  )

所以现在我只需要包含提到的 .h 文件。我将它们添加到我的其他源文件中:

ADD_EXECUTABLE ( MYProgramm
            ...
            #other source files
            ...
            src/svm_common.h
            src/svm_learn.h
)

不幸的是它不起作用。从这些 .h 文件调用任何函数都会导致链接器错误 LNK2001、LNK1120。 我猜我必须告诉 cmake 这些 .h 文件是 .o 文件的前端。但如何呢?

Hi basicly i am trying to use the svm from here . It is written in C and gives instructions how to use it in c++:

  • Compile "svm_learn.c", "svm_common.c", and "svm_hideo.c" as
    C code.
  • The C++ program you want to call svm_learn/8 and classify_example/2
    (or classify_example_linear/2) from
    needs to include the following
    headers:

    extern "C" {
    # include "svm_common.h"
    # include "svm_learn.h"
    }

  • Link "svm_learn.o", "svm_common.o", and "svm_hideo.o" to your program.

So i compiled the mentioned files and got the needed .o files.
Than i added:

SET( svm_lib_light_obj
    E:\framework\svm_light\build\svm_learn.o
    E:\framework\svm_light\build\svm_common.o
    E:\framework\svm_light\build\svm_hideo.o
)

ADD_LIBRARY(
    svm_lib_light
    STATIC
    EXCLUDE_FROM_ALL
    ${svm_lib_light_obj}
)

SET_SOURCE_FILES_PROPERTIES(
  ${svm_lib_light_obj}
  PROPERTIES
  EXTERNAL_OBJECT true # to say that "this is actually an object file, so it should not be compiled, only linked"
  GENERATED true       # to say that "it is OK that the obj-files do not exist before build time"
  )

SET_TARGET_PROPERTIES(
  svm_lib_light
  PROPERTIES
  LINKER_LANGUAGE C # Or else we get an error message, because cmake can't figure out from the ".o"-suffix that it is a C-linker we need.
  ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/lib
  )

So now i just need to include the .h files mentioned. I added them to my other source files in:

ADD_EXECUTABLE ( MYProgramm
            ...
            #other source files
            ...
            src/svm_common.h
            src/svm_learn.h
)

Unfortunatly it doesnt work. Calling any function from these .h files leads to an linker error LNK2001, LNK1120.
I am guessing i have to tell cmake that these .h files are frontends for the .o files. But how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诺曦 2024-10-21 05:08:14

最好的方法是将这些 C 文件添加到您的项目中:

SET(SVM_LIGHT_SRC_DIR "E:/framework/svm_light")

INCLUDE_DIRECTORIES(${SVM_LIGHT_SRC_DIR})

ADD_LIBRARY(
    svm_lib_light
    ${SVM_LIGHT_SRC_DIR}/svm_learn.c
    ${SVM_LIGHT_SRC_DIR}/svm_common.c
    ${SVM_LIGHT_SRC_DIR}/svm_hideo.c
)

ADD_EXECUTABLE ( MYProgramm
            ...
            #other source files
            ...
)

TARGET_LINK_LIBRARIES(MYProgram svm_lib_light)

The best way would be to add those C files to your project:

SET(SVM_LIGHT_SRC_DIR "E:/framework/svm_light")

INCLUDE_DIRECTORIES(${SVM_LIGHT_SRC_DIR})

ADD_LIBRARY(
    svm_lib_light
    ${SVM_LIGHT_SRC_DIR}/svm_learn.c
    ${SVM_LIGHT_SRC_DIR}/svm_common.c
    ${SVM_LIGHT_SRC_DIR}/svm_hideo.c
)

ADD_EXECUTABLE ( MYProgramm
            ...
            #other source files
            ...
)

TARGET_LINK_LIBRARIES(MYProgram svm_lib_light)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文