【发布时间】: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/…