CMake - 包含第三方文件

时间:2017-06-04 19:40:52

标签: 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)

2 个答案:

答案 0 :(得分:1)

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

target_link_libraries(
    NexusHookSubhook
    ${subhookLib}
    m
)   

其中subhookLib是subhook的库。

答案 1 :(得分:0)

我会推荐以下内容:

替换

type

#include "include/subhook/subhook.h"

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

#include "subhook.h"

仅包含.c文件作为SOURCE_FILES:

include_directories(include/subhook)