0

Here is part of my CMakeLists.txt:

ExternalProject_Add(
  subhook
  GIT_REPOSITORY https://github.com/Zeex/subhook.git
  GIT_TAG v0.8.2
  SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/subhook-src"
  BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/subhook-build"
  CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${EXTERNAL_INSTALL_LOCATION} -DSUBHOOK_STATIC=ON -DSUBHOOK_TESTS=OFF SUBHOOK_FORCE_32BIT=ON)

include_directories(${EXTERNAL_INSTALL_LOCATION}/include)
link_directories(${EXTERNAL_INSTALL_LOCATION}/lib)

add_executable(t_test t_test.cc)
add_dependencies(t_test subhook)

Here is my t_test.cc:

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <subhook.h>

bool success = false;
void Detoured_PushRecv(const std::string& msg) {
    success = true;
}

void test(const std::string& msg) {
    success = true;
}

TEST(SocketioClient, DispatcherTaskDispatch_SuccessfullyDispatch) {
    subhook::Hook hook_PushRecv((void *)test, (void *)Detoured_PushRecv, subhook::HookFlag64BitOffset);
    hook_PushRecv.Install();
}

I will get this error

t_test.obj : error LNK2019: unresolved external symbol __imp__subhook_new referenced in function ...

I cannot directly add

target_link_libraries(t_test subhook)

which may cause this error:

CMake Error at test/base/CMakeLists.txt:24 (target_link_libraries):
  Target "subhook" of type UTILITY may not be linked into another target.
  One may link only to INTERFACE, OBJECT, STATIC or SHARED libraries, or to
  executables with the ENABLE_EXPORTS property set.

I’m on windows. How can I fix this one?

Thanks everyone for reading.

Kiki
  • 1
  • You need to find out the exact **library file** which is created in the external project, and use `target_link_libraries` specifically for that file (or with the IMPORTED target which wraps that file). See [duplicate question](https://stackoverflow.com/questions/15175318/cmake-how-to-build-external-projects-and-include-their-targets) for more info. – Tsyvarev Sep 19 '22 at 10:18
  • @JesperJuhl: Given question has a **clear attempt** to link a library: a call to `target_link_libraries`. That attempt fails just because of **CMake** specifics. Please, do not close such questions as a duplicate for generic "undefined reference" question, which describes only C/C++ specific reasons. – Tsyvarev Sep 19 '22 at 10:27

0 Answers0