Linux C++ compile

c++ complie

1
2
3
g++ --std=c++11 changeFuYunRecord.cpp -o changefyrecord -g -O0 -lpthread

g++ --std=c++11 data_gen.cpp -o gen_data -g -O0 -lpthread

MakeFileDemo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
cmake_minimum_required (VERSION 2.8.8)
project (ddzpeiwan)

# cmake寻找cuda,这个要现在系统里面装好cuda,设置好cuda的环境参数啥的
FIND_PACKAGE(CUDA REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++17 -pthread -W ")
SET(CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS};-gencode arch=compute_70,code=sm_70;-std=c++11;)

link_directories(
/usr/local/lib/libjsoncpp.a
/home/user/wanran/lib
/home/user/wanran/TensorRT-5.1.5.0/lib
/usr/local/cuda-10.0/targets/x86_64-linux/lib/)

include_directories(
./include
/usr/src/tensorrt/samples/common
/home/user/wanran/include
/usr/local/include/eigen3
/usr/include/x86_64-linux-gnu
/usr/local/cuda-10.0/targets/x86_64-linux/include/
)

CUDA_ADD_EXECUTABLE(ddzpeiwan
main.cpp
aimodel.cpp
cfg_reader.cpp
util.cpp
logger.cpp
exception.cpp
ThreadMod.cpp
TreatorBase.cpp
truncation_cards.cpp
UnixTime.cpp
PollServer.cpp
TFService.cpp
ddz_utils.cpp)
target_link_libraries(ddzpeiwan tensorflow_cc tensorflow_framework cudart nvinfer nvparsers glog jsoncpp )

makefiles

cmake_minimum_required (VERSION 2.8.8)

project (ddzpeiwan)

FIND_PACKAGE

1
find_package(PythonLibs 3 REQUIRED)

include_directories

CUDA_ADD_EXECUTABLE

1
2
3
4
5
6
7
8
9
10
11
12
CUDA_ADD_EXECUTABLE( cuda_target file0 file1 ...
[WIN32] [MACOSX_BUNDLE] [EXCLUDE_FROM_ALL] [OPTIONS ...] )
-- Creates an executable "cuda_target" which is made up of the files
specified. All of the non CUDA C files are compiled using the standard
build rules specified by CMAKE and the cuda files are compiled to object
files using nvcc and the host compiler. In addition CUDA_INCLUDE_DIRS is
added automatically to include_directories(). Some standard CMake target
calls can be used on the target after calling this macro
(e.g. set_target_properties and target_link_libraries), but setting
properties that adjust compilation flags will not affect code compiled by
nvcc. Such flags should be modified before calling CUDA_ADD_EXECUTABLE,
CUDA_ADD_LIBRARY or CUDA_WRAP_SRCS.

https://cmake.org/cmake/help/v3.0/module/FindCUDA.html

target_link_libraries里库文件的顺序符合gcc链接顺序的规则,即被依赖的库放在依赖它的库的后面,比如

1
target_link_libraries(hello A B.a C.so)

在上面的命令中,libA.so可能依赖于libB.a和libC.so,如果顺序有错,链接时会报错。还有一点,B.a会告诉CMake优先使用静态链接库libB.a,C.so会告诉CMake优先使用动态链接库libC.so,也可直接使用库文件的相对路径或绝对路径。使用绝对路径的好处在 于,当依赖的库被更新时,make的时候也会重新链接。