【问题标题】:CMake - Include third party filesCMake - 包含第三方文件
【发布时间】:2017-06-04 19:40:52
【问题描述】:

我对使用第三方的东西很陌生,对使用 C 在 Windows 上工作也很陌生。

我目前正在尝试简单地使用并包含此third party library.
我已经下载了它并将所有文件放在我的主文件旁边的include/subhook/ 中。

我的main.c 看起来像github页面上的示例,除了它包含include/subhook/subhook.h

#include <stdio.h>
#include "include/subhook/subhook.h"

subhook_t foo_hook;

void foo(int x) {
    printf("real foo just got called with %d\n", x);
}

void my_foo(int x) {
    subhook_remove(foo_hook);

    printf("foo(%d) called\n", x);
    foo(x);

    subhook_install(foo_hook);
}

int main() {
    foo_hook = subhook_new((void *)foo, (void *)my_foo, 0);

    subhook_install(foo_hook);
    foo(123);

    subhook_remove(foo_hook);
    subhook_free(foo_hook);
}

这是我的 CMakeLists.txt 文件。我也尝试过包含所有其他 .c 文件,但它不起作用:

cmake_minimum_required(VERSION 3.7)
project(NexusHookSubhook)
set(CMAKE_C_STANDARD 11)

include_directories(include/subhook)

set(SOURCE_FILES main.c include/subhook/subhook.h include/subhook/subhook.c)
add_executable(NexusHookSubhook ${SOURCE_FILES})

当我尝试编译时,我会收到一大堆这些错误(我假设,这是由于链接/包含错误的库造成的)。谁能解释我在这里做错了什么?

C:\Users\Nakroma\.CLion2017.1\system\cygwin_cmake\bin\cmake.exe --build C:\Users\Nakroma\CLionProjects\NexusHookSubhook\cmake-build-debug --target NexusHookSubhook -- -j 4
[ 33%] Linking C executable NexusHookSubhook.exe
CMakeFiles/NexusHookSubhook.dir/main.c.o: In function `my_foo':
/cygdrive/c/Users/Nakroma/CLionProjects/NexusHookSubhook/main.c:12: undefined reference to `__imp_subhook_remove'
/cygdrive/c/Users/Nakroma/CLionProjects/NexusHookSubhook/main.c:12:(.text+0x3c): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__imp_subhook_remove'
/cygdrive/c/Users/Nakroma/CLionProjects/NexusHookSubhook/main.c:17: undefined reference to `__imp_subhook_install'
/cygdrive/c/Users/Nakroma/CLionProjects/NexusHookSubhook/main.c:17:(.text+0x69): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `__imp_subhook_install'
CMakeFiles/NexusHookSubhook.dir/main.c.o: In function `main':
/cygdrive/c/Users/Nakroma/CLionProjects/NexusHookSubhook/main.c:21: undefined reference to `__imp_subhook_new'
....

附加说明:我使用的是带有 Cygwin 2.8.0 和 CMake 3.7.2 的 Windows 10(使用 make 和 gcc 包以及 GDB 7.11.1)

【问题讨论】:

  • 重复说明:除了用include_directories指定headers目录外,还需要用target_link_libraries链接库文件。
  • 注意:使用add_subdirectory(include/subhook) 至少现在可以编译,但在运行时会出现错误error while loading shared libraries: cygsubhook.dll: cannot open shared object file: No such file or directory,尽管它在cmake-build-debug/include/subhook/ 中。移动它会导致成功编译和运行。知道如何自动将它放在那里吗?
  • 目前已解决。我用它把.dll复制到.exe目录stackoverflow.com/questions/10671916/…

标签: c windows cmake clion


【解决方案1】:

你完全错过了 CMakeFiles 中的链接部分

target_link_libraries(
    NexusHookSubhook
    ${subhookLib}
    m
)   

其中 subhookLib 是 subhook 的库。

【讨论】:

  • 这就是问题所在,这样做会导致cannot find -lsubhook。我的意思是,没有.so.dll
  • 提供的包里有dll吗?如果是,也许你必须添加 -L 来告诉你编译器的 dll 在哪里。
【解决方案2】:

我会推荐以下几点:

替换

#include "include/subhook/subhook.h"

#include "subhook.h"

因为路径的第一部分已经包含在这里:

include_directories(include/subhook)

仅将 .c 文件包含为 SOURCE_FILES:

set(SOURCE_FILES main.c include/subhook/subhook.c)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-03
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2020-03-04
    • 2023-04-10
    • 1970-01-01
    • 2017-04-17
    相关资源
    最近更新 更多