0

I want to import spdlog in my project using CMake. And I use VS Code. But my code doesn't work well. I think I don't have any problem in my code because I copy and paste all professor's code. I think that it is simple setting error, but I don't know the solution.

CMakeLists.txt

cmake_minimum_required(VERSION 3.13)

set(PROJECT_NAME cmake_project_example)
set(CMAKE_CXX_STANDARD 17)

project(${PROJECT_NAME})
add_executable(${PROJECT_NAME} src/main.cpp)

include(ExternalProject)

set(DEP_INSTALL_DIR ${PROJECT_BINARY_DIR}/install)
set(DEP_INCLUDE_DIR ${DEP_INSTALL_DIR}/include)
set(DEP_LIB_DIR ${DEP_INSTALL_DIR}/lib)

ExternalProject_Add(
    dep-spdlog
    GIT_REPOSITORY "https://github.com/gabime/spdlog.git"
    GIT_TAG "v1.x"
    GIT_SHALLOW 1
    UPDATE_COMMAND ""
    PATCH_COMMAND ""
    CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${DEP_INSTALL_DIR}
    TEST_COMMAND "" 
)

set(DEP_LIST ${DEP_LIST} dep-spdlog)
set(DEP_LIBS ${DEP_LIBS} spdlog$<$<CONFIG:Debug>:d>)

target_include_directories(${PROJECT_NAME} PUBLIC ${DEP_INCLUDE_DIR})
target_link_directories(${PROJECT_NAME} PUBLIC ${DEP_LIB_DIR})
target_link_libraries(${PROJECT_NAME} PUBLIC ${DEP_LIBS})

add_dependencies(${PROJECT_NAME} ${DEP_LIST})

main.cpp

#include <spdlog/spdlog.h>

int main(int argc, const char** argv) {
    SPDLOG_INFO("Hello, world!");
    return 0;
}

Heres the output:

build Output
[proc] Executing command: D:\Program\CMake\bin\cmake.EXE --build d:/Project/real_cmake/build --config Debug --target all -j 26 --
[build] [  9%] Performing build step for 'dep-spdlog'
[build] [ 80%] Built target spdlog
[build] [ 90%] Building CXX object example/CMakeFiles/example.dir/example.cpp.obj
[build] In file included from D:/Project/real_cmake/build/dep-spdlog-prefix/src/dep-spdlog/include/spdlog/sinks/udp_sink.h:10,
[build]                  from D:\Project\real_cmake\build\dep-spdlog-prefix\src\dep-spdlog\example\example.cpp:243:
[build] D:/Project/real_cmake/build/dep-spdlog-prefix/src/dep-spdlog/include/spdlog/details/udp_client-windows.h: In constructor 'spdlog::details::udp_client::udp_client(const string&, uint16_t)':
# **[build] D:/Project/real_cmake/build/dep-spdlog-prefix/src/dep-spdlog/include/spdlog/details/udp_client-windows.h:67:13: error: 'InetPtonA' was not declared in this scope  <<<<<< I think this error is critical
# **[build]          if (InetPtonA(PF_INET, host.c_str(), &addr_.sin_addr.s_addr) != 1)
[build]              ^~~~~~~~~
[build] D:/Project/real_cmake/build/dep-spdlog-prefix/src/dep-spdlog/include/spdlog/details/udp_client-windows.h:67:13: note: suggested alternative: 'GetPropA'
[build]          if (InetPtonA(PF_INET, host.c_str(), &addr_.sin_addr.s_addr) != 1)
[build]              ^~~~~~~~~
[build]              GetPropA
[build] mingw32-make.exe[5]: *** [example\CMakeFiles\example.dir\build.make:76: example/CMakeFiles/example.dir/example.cpp.obj] Error 1
[build] mingw32-make.exe[4]: *** [CMakeFiles\Makefile2:125: example/CMakeFiles/example.dir/all] Error 2
[build] mingw32-make.exe[3]: *** [Makefile:155: all] Error 2
[build] mingw32-make.exe[2]: *** [CMakeFiles\dep-spdlog.dir\build.make:86: dep-spdlog-prefix/src/dep-spdlog-stamp/dep-spdlog-build] Error 2
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:110: CMakeFiles/dep-spdlog.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:90: all] Error 2
[proc] The command: D:\Program\CMake\bin\cmake.EXE --build d:/Project/real_cmake/build --config Debug --target all -j 26 -- exited with code: 2
[build] Build finished with exit code 2

I tried searching the error on internet, but I can't find any information for this problem. I don't have any knowledge about CMake and spdlog.

starball
  • 20,030
  • 7
  • 43
  • 238
  • Looks similar to [that bugreport](https://github.com/gabime/spdlog/issues/2225). Inclusion of Windows-specific header `include/spdlog/details/udp_client-windows.h` is protected with `_WIN32` macro (https://github.com/gabime/spdlog/blob/v1.x/include/spdlog/sinks/udp_sink.h#L10), which is defined under MinGW too. Maybe your MinGW toolchain is too old to define that function. – Tsyvarev Feb 07 '23 at 10:15
  • I use the MinGW's latest version.... And there is windows.h in the path ( D:\mingw64\x86_64-w64-mingw32\include\windows.h ). I have been frustrated for 3 days because of this problem. omg – Yujeong Feb 07 '23 at 11:30
  • The function `InetPtonA` should be provided by the header `ws2tcpip.h`. You may check that given header in your MinGW toolchain actually provides given function. – Tsyvarev Feb 07 '23 at 11:41
  • Thank you for help, but that file also exists in D:\mingw64\x86_64-w64-mingw32\include. I really get confused...... OTL – Yujeong Feb 07 '23 at 12:35
  • Again, the problem is NOT about **existence** of the **header** file. Would the file does not exist, the compiler would emit the error about that fact. The problem is about declaration of the **function** `InetPtonA` in that file. – Tsyvarev Feb 07 '23 at 12:40
  • This seems to be a winsock2 function. The order of includes can be problematic, so this may be related to the following issue: https://stackoverflow.com/questions/1372480/c-redefinition-header-files-winsock2-h – fabian Feb 07 '23 at 18:37

0 Answers0