Skip to content

Commit 8cca2e4

Browse files
authored
Merge pull request #3 from mfl28/cmake-modular
Make cmake project modular
2 parents 6ce25d0 + fb9dcb9 commit 8cca2e4

File tree

6 files changed

+151
-16
lines changed

6 files changed

+151
-16
lines changed

CMakeLists.txt

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
3-
project(pytorch-cpp)
42

3+
project(pytorch-cpp VERSION 1.0.0 LANGUAGES CXX)
54

65
find_package(Torch REQUIRED)
76

8-
add_executable(pytorch-cpp main.cpp)
9-
target_link_libraries(pytorch-cpp "${TORCH_LIBRARIES}")
7+
set(EXECUTABLE_NAME pytorch-cpp)
8+
add_executable(${EXECUTABLE_NAME} main.cpp)
9+
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
10+
CXX_STANDARD 11
11+
CXX_STANDARD_REQUIRED YES
12+
)
1013

11-
# add_executable(pytorch_basics tutorials/basics/pytorch_basics/main.cpp)
12-
# target_link_libraries(pytorch_basics "${TORCH_LIBRARIES}")
13-
add_executable(linear_regression tutorials/basics/linear_regression/main.cpp)
14-
target_link_libraries(linear_regression "${TORCH_LIBRARIES}")
15-
# add_executable(logistic_regression tutorials/basics/logistic_regression/main.cpp)
16-
# target_link_libraries(logistic_regression "${TORCH_LIBRARIES}")
17-
# add_executable(feedforward_neural_network tutorials/basics/feedforward_neural_network/main.cpp)
18-
# target_link_libraries(feedforward_neural_network "${TORCH_LIBRARIES}")
14+
target_link_libraries(${EXECUTABLE_NAME} "${TORCH_LIBRARIES}")
1915

20-
set_property(TARGET pytorch-cpp PROPERTY CXX_STANDARD 11)
16+
# Add tutorial projects:
17+
add_subdirectory("tutorials/basics/feedforward_neural_network")
18+
add_subdirectory("tutorials/basics/linear_regression")
19+
add_subdirectory("tutorials/basics/logistic_regression")
20+
add_subdirectory("tutorials/basics/pytorch_basics")
21+
22+
# The following code block is suggested to be used on Windows.
23+
# According to https://github.com/pytorch/pytorch/issues/25457,
24+
# the DLLs need to be copied to avoid memory errors.
25+
# See https://pytorch.org/cppdocs/installing.html.
26+
if (MSVC)
27+
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
28+
add_custom_command(TARGET ${EXECUTABLE_NAME}
29+
POST_BUILD
30+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
31+
${TORCH_DLLS}
32+
$<TARGET_FILE_DIR:${EXECUTABLE_NAME}>)
33+
endif (MSVC)

scripts.sh

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
function install() {
44
wget https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip
55
unzip libtorch-shared-with-deps-latest.zip
6-
rm -rf libtorch-shared-with-deps-latest.zip
6+
rm -rf libtorch-shared-with-deps-latest.zip
77
}
88

99
function build() {
@@ -15,11 +15,13 @@ function build() {
1515
}
1616

1717
function lint() {
18-
cpplint --linelength=120 main.cpp tutorials/*/*/**
18+
cpplint --linelength=120 --recursive \
19+
--filter=-build/include_subdir,-build/include_what_you_use main.cpp tutorials/*/*/**
1920
}
2021

2122
function lintci() {
22-
python cpplint.py --linelength=120 main.cpp tutorials/*/*/**
23+
python cpplint.py --linelength=120 --recursive \
24+
--filter=-build/include_subdir,-build/include_what_you_use main.cpp tutorials/*/*/**
2325
}
2426

2527
if [ $1 = "install" ]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2+
3+
project(feedforward-neural-network VERSION 1.0.0 LANGUAGES CXX)
4+
5+
#find_package(Torch REQUIRED)
6+
7+
set(SOURCES main.cpp)
8+
9+
set(EXECUTABLE_NAME feedforward-neural-network)
10+
11+
add_executable(${EXECUTABLE_NAME} ${SOURCES})
12+
target_link_libraries(${EXECUTABLE_NAME} "${TORCH_LIBRARIES}")
13+
14+
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
15+
CXX_STANDARD 11
16+
CXX_STANDARD_REQUIRED YES
17+
)
18+
19+
# The following code block is suggested to be used on Windows.
20+
# According to https://github.com/pytorch/pytorch/issues/25457,
21+
# the DLLs need to be copied to avoid memory errors.
22+
# See https://pytorch.org/cppdocs/installing.html.
23+
if (MSVC)
24+
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
25+
add_custom_command(TARGET ${EXECUTABLE_NAME}
26+
POST_BUILD
27+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
28+
${TORCH_DLLS}
29+
$<TARGET_FILE_DIR:${EXECUTABLE_NAME}>)
30+
endif (MSVC)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2+
3+
project(linear-regression VERSION 1.0.0 LANGUAGES CXX)
4+
5+
#find_package(Torch REQUIRED)
6+
7+
set(SOURCES main.cpp)
8+
9+
set(EXECUTABLE_NAME linear-regression)
10+
11+
add_executable(${EXECUTABLE_NAME} ${SOURCES})
12+
target_link_libraries(${EXECUTABLE_NAME} "${TORCH_LIBRARIES}")
13+
14+
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
15+
CXX_STANDARD 11
16+
CXX_STANDARD_REQUIRED YES
17+
)
18+
19+
# The following code block is suggested to be used on Windows.
20+
# According to https://github.com/pytorch/pytorch/issues/25457,
21+
# the DLLs need to be copied to avoid memory errors.
22+
# See https://pytorch.org/cppdocs/installing.html.
23+
if (MSVC)
24+
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
25+
add_custom_command(TARGET ${EXECUTABLE_NAME}
26+
POST_BUILD
27+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
28+
${TORCH_DLLS}
29+
$<TARGET_FILE_DIR:${EXECUTABLE_NAME}>)
30+
endif (MSVC)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2+
3+
project(logistic-regression VERSION 1.0.0 LANGUAGES CXX)
4+
5+
#find_package(Torch REQUIRED)
6+
7+
set(SOURCES main.cpp)
8+
9+
set(EXECUTABLE_NAME logistic-regression)
10+
11+
add_executable(${EXECUTABLE_NAME} ${SOURCES})
12+
target_link_libraries(${EXECUTABLE_NAME} "${TORCH_LIBRARIES}")
13+
14+
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
15+
CXX_STANDARD 11
16+
CXX_STANDARD_REQUIRED YES
17+
)
18+
19+
# The following code block is suggested to be used on Windows.
20+
# According to https://github.com/pytorch/pytorch/issues/25457,
21+
# the DLLs need to be copied to avoid memory errors.
22+
# See https://pytorch.org/cppdocs/installing.html.
23+
if (MSVC)
24+
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
25+
add_custom_command(TARGET ${EXECUTABLE_NAME}
26+
POST_BUILD
27+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
28+
${TORCH_DLLS}
29+
$<TARGET_FILE_DIR:${EXECUTABLE_NAME}>)
30+
endif (MSVC)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2+
3+
project(pytorch-basics VERSION 1.0.0 LANGUAGES CXX)
4+
5+
#find_package(Torch REQUIRED)
6+
7+
set(SOURCES main.cpp)
8+
9+
set(EXECUTABLE_NAME pytorch-basics)
10+
11+
add_executable(${EXECUTABLE_NAME} ${SOURCES})
12+
target_link_libraries(${EXECUTABLE_NAME} "${TORCH_LIBRARIES}")
13+
14+
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
15+
CXX_STANDARD 11
16+
CXX_STANDARD_REQUIRED YES
17+
)
18+
19+
# The following code block is suggested to be used on Windows.
20+
# According to https://github.com/pytorch/pytorch/issues/25457,
21+
# the DLLs need to be copied to avoid memory errors.
22+
# See https://pytorch.org/cppdocs/installing.html.
23+
if (MSVC)
24+
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
25+
add_custom_command(TARGET ${EXECUTABLE_NAME}
26+
POST_BUILD
27+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
28+
${TORCH_DLLS}
29+
$<TARGET_FILE_DIR:${EXECUTABLE_NAME}>)
30+
endif (MSVC)

0 commit comments

Comments
 (0)