CMake - 包含第三方文件

标签 c windows cmake clion

我不熟悉使用第三方的东西,也不熟悉使用 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'
....

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

最佳答案

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

target_link_libraries(
    NexusHookSubhook
    ${subhookLib}
    m
)   

其中subhookLib是subhook的库

关于CMake - 包含第三方文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44358085/

相关文章:

cmake - 获取具有指定前缀的变量列表

c++ - 允许在 Cmake 中相互依赖

c - 如何在C中获得最大的小数精度

使用 `pthread_create` 创建用户级线程或内核级线程?

c - 带符号的十六进制字符串到 long int 函数

java - Jenkins 构建不会在应该失败的时候失败

windows - 较低的过滤器驱动程序

cmake - 如何使用 HTTPS URL 请求 GIT_REPOSITORY 的密码

c - 将数组保存到文件不起作用,程序崩溃

windows - 剪贴板是否在 Windows 下的桌面之间共享?