Linux下编译生成动态链接库*.so
CMakeLists.txt和Makefile
将这几个文件(.h文件放在一个目录下)编译成一个动态库:libtest.so
$ gcc test_a.c test_b.c test_c.c -fPIC -shared -o libtest.so
将test.c(main函数文件)与动态库libtest.so链接生成执行文件test:
$ gcc test.c -L. -ltest -o test
测试是否动态连接,如果列出libtest.so,那么应该是连接正常了
$ ldd test
执行test
通过修改 LD_LIBRARY_PATH或者/etc/ld.so.conf文件来指定动态库的目录。通常这样做就可以解决库无法链接的问题了。
export LD_LIBRARY_PATH=/opt/au1200_rm/build_tools/bin: $LD_LIBRARY_PATH
http://blog.sina.com.cn/s/blog_54f82cc20101153x.html
将.cpp(除去main.cpp)和.h文件放在一个目录下,编译生成.so文件(库文件)
gcc -std=c++11 test_a.cpp test_b.cpp test_c.cpp -fPIC -shared -o libtest.so
将.so文件、main.cpp和.h文件放在一个目录下,编译生成bin文件(二进制可执行文件)
g++ main.cpp -std=c++11 -I. facedetectcnn.h scenedetect.h sdr2hdr.h -L. libdetect.so -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_ml -lpthread -o detect
编译c++命令记录:
g++ -O2 test_ve.cpp -std=c++11 -I../cmake/include -I../platform -I/root/anaconda3/envs/chl_trt/lib/python3.6/site-packages/numpy/core/include -I../build/opencv/local/include/opencv4 -L../cmake/lib -lve -lopencv_world -lsecurec -I/root/anaconda3/envs/chl_trt/include/python3.6m -L/root/anaconda3/envs/chl_trt/lib/ -lpython3.6m -o test_ve
g++ main.cpp -std=c++11 -I. facedetectcnn.h -L. libfacedetection.so -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_ml -lopencv_videoio -o demo
用这个:
g++ main.cpp -std=c++11 -I. facedetectcnn.h -L. libfacedetection.so -L/usr/local/lib -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_ml -o facedetect
Linux下直接编译生成可执行文件
单个源文件生成可执行程序:
通过 -o 选项指定可执行程序的文件名。下面的命令将产生名为 helloworld 的可执行文件:
g++ helloworld.cpp -o helloworld
在命令行中输入程序名可使之运行:
./helloworld
多个源文件生成可执行程序:
g++ hellospeak.cpp speak.cpp -o hellospeak
https://blog.youkuaiyun.com/dengshuai_super/article/details/51766786
包括opencv
g++ scene.cpp -std=c++11 -o scenedetect `pkg-config --libs --cflags opencv` -ldl
报错:
template with C linkage
错误的将头文件写在了extern "C" 里面,所以会出现该错误。
https://blog.youkuaiyun.com/u012175637/article/details/81561768
编译错误
error: 'uint8_t' does not name a type
加入
#include <stdint.h>
就好了。
http://www.voidcn.com/article/p-qnuawirx-cd.html
https://blog.youkuaiyun.com/commshare/article/details/17892665
GCC 编译优化级别 -o1 -o2 -o3等
gcc -fdefer-pop -o test test.c
gcc -O1 -fno-defer-pop -o test test.c #启用所有O1的优化选项除了defer-pop
https://www.lenzhao.com/topic/59111ed6fe30978105e0902e
我们通常用-O2编译,因为-O3会“触发微妙的错误”.
http://www.voidcn.com/article/p-yqeqazqx-buv.html
动态链接库 .so
静态链接库 .o
编译:gcc hello.c -fPIC -o libhello.so
其中-fPIC选项的作用是:表示编译为位置独立的代码,不用此选项的话编译后的代码是位置相关的
创建hello.so动态库 (创建库)
编译:gcc -fPIC -shared hello.c -o libhello.so
链接动态库 (链接库)
编译:gcc main.c -L. -lhello -o main
这里-L的选项是指定编译器在搜索动态库时搜索的路径,告诉编译器hello库的位置。 "."意思是当前路径.
https://blog.youkuaiyun.com/a600423444/article/details/7206015
https://www.cnblogs.com/fence/archive/2009/09/29/1576503.html
cmake学习
在Ubuntu下编译CUDA示例时,为什么找不到libcudart.so.4?
https://ubuntuqa.com/article/9518.html
在CMake中将文件添加到source_group
http://www.voidcn.com/article/p-tgtshvdi-bve.html
Cmakelist: add_library、target_link_libraries和link_directories用法
https://blog.youkuaiyun.com/wfei101/article/details/82633858
Make手册详解 (十)
https://www.cnblogs.com/coderfenghc/archive/2012/07/08/2581734.html
CMakeLists.txt和Makefile
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件
CMake的所有的语句都写在一个叫:CMakeLists.txt 的文件中。当CMakeLists.txt文件确定后,可以用cmake命令对相关 的变量值进行配置。这个命令必须指向CMakeLists.txt所在的目录。配置完成之后,应用cmake命令生成相应的makefile(在Unix like系统下)
先编写一个CMakeLists.txt文件,将需要的.h和.cpp文件包含进来,然后在不同的平台使用CMake调用各自的编译器生成各自的工程。
Makefile是linux下面的文件
它将所需要的命令都包含在这个Makefile文件中,然后简单的make一下就完成了所有的编译步骤。
使用:
$ mkdir build
$ cd build
$ cmake .. // .. 表示CMakeLists.txt文件在build的上一层目录, cmake 生成makefile文件(用cmake命令对相关 的变量值进行配置)
($ make clean)
$ make // 编译、链接、生成可执行文件等
($ make install)
https://blog.youkuaiyun.com/jay463261929/article/details/53859616
用CMake混合编译C++与cuda
CMakeLists.txt:
# CMakeLists.txt for G4CU project
project(gpu)
# required cmake version
cmake_minimum_required(VERSION 2.8)
# packages
find_package(CUDA)
#include_directories ("${PROJECT_SOURCE_DIR}")
# nvcc flags -g for debug
#set(CUDA_NVCC_FLAGS -O3;-G;-g)
#set(CUDA_NVCC_FLAGS -gencode arch=compute_20,code=sm_20;-G;-g)
#set(CUDA_NVCC_FLAGS -gencode arch=compute_52,code=sm_52;-G;-g)
file(GLOB_RECURSE CURRENT_HEADERS *.h *.hpp *.cuh)
file(GLOB CURRENT_SOURCES *.cpp *.cu)
source_group("Include" FILES ${CURRENT_HEADERS})
source_group("Source" FILES ${CURRENT_SOURCES})
#cuda_add_library(gpu SHARED ${CURRENT_HEADERS} ${CURRENT_SOURCES})
cuda_add_library(gpu STATIC ${CURRENT_HEADERS} ${CURRENT_SOURCES})
https://blog.youkuaiyun.com/fb_help/article/details/79330815
例子
https://blog.youkuaiyun.com/comedate/article/details/109347874?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.baidujs&dist_request_id=969a50e3-1ad3-4dbb-b108-eacf4af8e6dc&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.baidujs
nvcc是编译cuda程序的编译器,CDUA C是在C语言上的扩展,所以它依赖C编译器(C编译器在window下是cl.exe,在Linux下是gcc)。因此我们编译CUDA程序必须依靠编译器nvcc。
其实,nvcc编译cuda程序和g++编译c++程序是差不多的。
https://blog.youkuaiyun.com/fb_help/article/details/79283032
linux编译openmp
gcc -fopenmp filename.c -o filename
https://blog.youkuaiyun.com/qq_20198487/article/details/51626464
编译可视化
make VERBOSE=1
编译错误
g++ first.cpp -o first `pkg-config --libs --cflags opencv` -ldl
https://blog.youkuaiyun.com/cainiaohudi/article/details/80375557
error: ‘to_string’ was not declared in this scope
加入编译选项-std=c++11
https://blog.youkuaiyun.com/farmwang/article/details/74309004
C++之‘nullptr’ was not declared in this scope
g++ -std=gnu++0x int.cpp -o int
https://blog.youkuaiyun.com/u011068702/article/details/64906864
g++编译goto语句出现:[error:jump to label XXX]
http://www.voidcn.com/article/p-fattkiil-bdx.html
error: ‘stoi’ was not declared in this scope
g++ filename.cpp -std=c++11
https://upliu.net/error-stoi-was-not-declared-in-this-scope.html
error: no matching function for call to 'std::basic_ifstream::open(std::string&)
error: no matching function for call to 'std::basic_ifstream::open(std::string&)
可以试下
filename.c_str()
https://blog.youkuaiyun.com/cs_zlg/article/details/8300124
需要把string类型的转换到const char*,string类型转换为const char* 只需在后面添加.c_str()即可。
https://blog.youkuaiyun.com/KUAILE123/article/details/20942687
make: nvcc: Command not found
vim ~/.bashrc
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
source ~/.bashrc
https://blog.youkuaiyun.com/Flying_sfeng/article/details/103343813