2017-06-04 46 views
0

我是很新,与第三方的东西,也正在新Windows系统,与CCMake的 - 包括第三方文件

我目前正试图简单地使用,包括这个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和3.7.2的CMake(使用make和gcc包和GDB 7.11.1)

+0

可能的重复[CMake链接到外部库](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library) – Tsyvarev

+0

重复的解释:除了用'include_directories'指定头文件目录外,您需要使用'target_link_libraries'链接库文件。 – Tsyvarev

+0

注意:使用'add_subdirectory(include/subhook)'它现在至少编译了,但是运行时它给出错误'加载共享库时出错:cygsubhook.dll:无法打开共享目标文件:没有这样的文件或目录,它在'cmake-build-debug/include/subhook /'中。移动它会导致成功编译和运行。任何想法如何自动把它放在那里? – nn3112337

回答

1

你完全错过你CMakeFiles联结部

target_link_libraries(
    NexusHookSubhook 
    ${subhookLib} 
    m 
) 

其中subhookLib是子钩库。

+0

虽然这是问题,但这样做会导致'找不到-lsubhook'。 我的意思是,没有“.so”或“.dll” – nn3112337

+0

包装中是否有任何dll?如果是,也许你必须添加-L 来告诉你编译器在哪里。 – Gaulois94

0

我建议以下几点:

替换

#include "include/subhook/subhook.h" 

#include "subhook.h" 

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

include_directories(include/subhook) 

只有包括.c文件为SOURCE_FILES:

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