|
1 |
| -cmake_minimum_required(VERSION 3.0 FATAL_ERROR) |
2 |
| -project("dcgan") |
| 1 | +cmake_minimum_required(VERSION 3.20 FATAL_ERROR) |
3 | 2 |
|
4 |
| -set(Torch_DIR "C:/libtorch/") |
5 |
| -set(CMAKE_PREFIX_PATH "C:/libtorch/share/cmake/") |
| 3 | +project("dcgan") |
6 | 4 |
|
| 5 | +# Ensure the path to libtorch is correct. Adjust it according to your installation. |
| 6 | +set(Torch_DIR "D:/include/libtorch/share/cmake/Torch") |
| 7 | +set(Torch_INCLUDE_DIRS "D:/include/libtorch/include") |
7 | 8 | find_package(Torch REQUIRED)
|
8 | 9 |
|
9 |
| -add_executable(dcgan "dcgan.cpp") |
| 10 | +# Include the Torch headers |
| 11 | +include_directories(${Torch_INCLUDE_DIRS}) |
10 | 12 |
|
11 |
| -target_link_libraries(dcgan |
12 |
| - "${TORCH_LIBRARIES}" |
13 |
| - "C:/libtorch/lib/c10.dll" |
14 |
| - "C:/libtorch/lib/torch_cpu.dll" |
15 |
| -) |
| 13 | +# Set any necessary compile flags |
| 14 | +if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") |
| 15 | + # Compiler is Microsoft Visual C++ |
| 16 | + # Set MSVC-specific flags |
| 17 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /Zc:__cplusplus /EHsc /std:c++latest /experimental:module") |
| 18 | +endif() |
16 | 19 |
|
| 20 | +if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") |
| 21 | + # Compiler is g++ |
| 22 | + # Set g++-specific flags |
| 23 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") |
| 24 | +endif() |
| 25 | + |
| 26 | +add_executable(dcgan "dcgan.cpp") |
| 27 | +target_link_libraries(dcgan "${TORCH_LIBRARIES}") |
17 | 28 | set_property(TARGET dcgan PROPERTY CXX_STANDARD 20)
|
| 29 | + |
| 30 | +# The following code block is suggested to be used on Windows. |
| 31 | +# According to https://github.com/pytorch/pytorch/issues/25457, |
| 32 | +# the DLLs need to be copied to avoid memory errors. |
| 33 | +if(MSVC) |
| 34 | + file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll") |
| 35 | + add_custom_command(TARGET dcgan |
| 36 | + POST_BUILD |
| 37 | + COMMAND ${CMAKE_COMMAND} -E copy_if_different |
| 38 | + ${TORCH_DLLS} |
| 39 | + $<TARGET_FILE_DIR:dcgan>) |
| 40 | +endif(MSVC) |
0 commit comments