CMake - 包含第三方文件

CMake - Include third party files

我对使用第三方的东西很陌生,对使用 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})

当我尝试编译时,我得到了一大堆这些错误(我假设,linking/including 库错误)。谁能解释一下我在这里做错了什么?

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

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

我会推荐以下内容:

替换

#include "include/subhook/subhook.h"

#include "subhook.h"

因为路径的第一部分已包含在此处:

include_directories(include/subhook)

仅将 .c 文件包含为 SOURCE_FILES:

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

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

target_link_libraries(
    NexusHookSubhook
    ${subhookLib}
    m
)   

其中subhookLib是subhook的库