Make and Cmake

1. cmake

1.1. Find CUDA

find_package(CUDAToolkit [] [QUIET] [REQUIRED] [EXACT] […])

Some default environment variable will be searched, such as CUDAToolkit_ROOT. More information

1.2. The difference between LD_LIBRARY_PATH and LIBRARY_PATH

LD_LIBRARY_PATH enables the system to locate dynamic libraries during runtime. LIBRARY_PATH directs the compiler to locate dynamic libraries during compilation.

1.3. Libtorch

The CmakeLists.txt is as follow.

1
2
3
4
5
6
7
8
9
cmake_minimum_required(VERSION 3.10 FATAL_ERROR)
project(custom_ops)

set(CMAKE_CUDA_COMPILER /opt/cuda/bin/nvcc)
find_package(Torch REQUIRED)

add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 17)

It will use /usr/bin/gcc and /usr/bin/g++ to compile the source files. When I add

1
2
set(CMAKE_C_COMPILER /usr/bin/gcc-13)
set(CMAKE_CXX_COMPILER /usr/bin/g++-13)

into the CmakeLists.txt, it reports The C compiler identification is GNU 13.3.0. However, it also use /usr/bin/gcc and /usr/bin/g++ to compile the source files. Only if i rm /usr/bin/g++, and ln -s /usr/bin/g++-13 /usr/bin/g++, it will use g++13.

The path of standard include directory for g++ is embeded in the binary file of g++, and it has nothing with CmakeLists.txt.

2. make

2.1. Some Symbols

$@ means the target file

$^ means all the dependences

$< means the first dependence

$? means the dependences newer than the target file