1

I'm pretty new to working with third party stuff and also new to working on Windows with C.

I'm currently trying to simply use and include this third party library.
I've downloaded it and put all the files in include/subhook/ next to my main file.

My main.c looks like the example on the github page, except it includes 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);
}

and this is my CMakeLists.txt file. I've also tried including all the other .c files but it won't work:

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})

When I try to compile I get a whole load of these errors (which is, I assume, from linking/including the library wrong). Can anyone explain what I'm doing wrong here?

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

Additional notes: I'm on Windows 10 with Cygwin 2.8.0 and CMake 3.7.2 (using the make and gcc package and GDB 7.11.1)

nn3112337
  • 459
  • 6
  • 21
  • Possible duplicate of [CMake link to external library](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library) – Tsyvarev Jun 04 '17 at 19:48
  • Duplicate explanations: aside from specifying headers directory with `include_directories`, you need to link with the library file with `target_link_libraries`. – Tsyvarev Jun 04 '17 at 19:49
  • Note: with `add_subdirectory(include/subhook)` it at least compiles now, but on running it gives the error `error while loading shared libraries: cygsubhook.dll: cannot open shared object file: No such file or directory`, altough it is in `cmake-build-debug/include/subhook/`. Moving it results in successfully compiling and running. Any idea how to automatically place it there? – nn3112337 Jun 04 '17 at 20:08
  • Solved so far. I've used this to copy the .dll into the .exe directory https://stackoverflow.com/questions/10671916/how-to-copy-dll-files-into-the-same-folder-as-the-executable-using-cmake – nn3112337 Jun 04 '17 at 20:35

2 Answers2

1

you totally miss the linking part in your CMakeFiles

target_link_libraries(
    NexusHookSubhook
    ${subhookLib}
    m
)   

Where subhookLib is the library of subhook.

Gaulois94
  • 159
  • 9
  • That's the problem though, doing that results in `cannot find -lsubhook`. What I mean is, there is no `.so` or `.dll` – nn3112337 Jun 04 '17 at 19:56
  • Is there any dll in the package furnished ? If yes maybe you have to add -L to tell you compiler where the dlls are. – Gaulois94 Jun 04 '17 at 20:20
0

I would recommend the following things:

Replace

#include "include/subhook/subhook.h"

with

#include "subhook.h"

as first part of the path is already included over here:

include_directories(include/subhook)

Only include the .c files as SOURCE_FILES:

set(SOURCE_FILES main.c include/subhook/subhook.c)
Rajanya Dhar
  • 166
  • 6