Configrations
~/.bashrc
:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/username/local/opencv-3.4.7/lib
Compile
demo
#include <opencv2/opencv.hpp>
#include <iostream>
// using namespace cv;
int main()
{
cv::Mat img=cv::imread("man.jpg");
cv::imshow("image",img);
cv::waitKey();
return 0;
}
- using
pkg-config
g++ test.cpp `pkg-config --cflags --libs opencv` -o Test
gcc main.c `pkg-config --cflags --libs gtk+-2.0` -o main
~/.bashrc
:
export PKG_CONFIG_PATH=/home/username/local/opencv-3.4.7/lib/pkgconfig:$PKG_CONFIG_PATH
/home/username/local/opencv-3.4.7/lib/pkgconfig/opencv.pc
:
# Package Information for pkg-config
prefix=/home/username/local/opencv-3.4.7
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv
includedir_new=${prefix}/include
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 3.4.7
Libs: -L${exec_prefix}/lib -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_aruco -lopencv_bgsegm -lopencv_bioinspired -lopencv_ccalib -lopencv_cvv -lopencv_dnn_objdetect -lopencv_dpm -lopencv_face -lopencv_freetype -lopencv_fuzzy -lopencv_hdf -lopencv_hfs -lopencv_img_hash -lopencv_line_descriptor -lopencv_optflow -lopencv_reg -lopencv_rgbd -lopencv_saliency -lopencv_sfm -lopencv_stereo -lopencv_structured_light -lopencv_phase_unwrapping -lopencv_surface_matching -lopencv_tracking -lopencv_datasets -lopencv_text -lopencv_highgui -lopencv_videoio -lopencv_dnn -lopencv_plot -lopencv_xfeatures2d -lopencv_shape -lopencv_video -lopencv_ml -lopencv_ximgproc -lopencv_xobjdetect -lopencv_objdetect -lopencv_calib3d -lopencv_imgcodecs -lopencv_features2d -lopencv_flann -lopencv_xphoto -lopencv_photo -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt -L/usr/lib/x86_64-linux-gnu -lGL -lGLU
Cflags: -I${includedir_old} -I${includedir_new}
g++ `pkg-config --cflags --libs opencv` test.cpp -o Test
- directly use g++ command:
g++ -I(include path) -L(lib path) -l(which libxxx.so) sourefile.cpp -o a.out
-I/usr/local/Cellar/opencv3/3.1.0_1/include/opencv
-I/usr/local/Cellar/opencv3/3.1.0_1/include
-L/usr/local/Cellar/opencv3/3.1.0_1/lib
-lopencv_shape
-lopencv_stitching
-lopencv_objdetect
-lopencv_superres
-lopencv_videostab
-lopencv_flann
-lopencv_calib3d
-lopencv_features2d
-lopencv_videoio
-lopencv_imgcodecs
-lopencv_video
-lopencv_photo
-lopencv_ml
-lopencv_imgproc
-lopencv_core
-lopencv_highgui
username@HOME:~/wk/exp6/scc$ g++ main.cpp -o a.out -I/home/username/local/opencv-3.4.7/include -L/home/username/local/opencv-3.4.7/lib/ -lopencv_core
/tmp/ccje9Ael.o: In function `main':
main.cpp:(.text+0x48): undefined reference to `cv::imread(cv::String const&, int)'
main.cpp:(.text+0x9c): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
main.cpp:(.text+0xc4): undefined reference to `cv::waitKey(int)'
collect2: error: ld returned 1 exit status
username@HOME:~/wk/exp6/scc$ g++ main.cpp -o a.out -I/home/username/local/opencv-3.4.7/include -L/home/username/local/opencv-3.4.7/lib/ -lopencv_core -lopencv_highgui
/usr/bin/ld: /tmp/cclltTat.o: undefined reference to symbol '_ZN2cv6imreadERKNS_6StringEi'
//home/username/local/opencv-3.4.7/lib/libopencv_imgcodecs.so.3.4: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
username@HOME:~/wk/exp6/scc$ g++ main.cpp -o a.out -I/home/username/local/opencv-3.4.7/include -L/home/username/local/opencv-3.4.7/lib/ -lopencv_core -lopencv_highgui -lopencv_imgproc
/usr/bin/ld: /tmp/ccpwjZbD.o: undefined reference to symbol '_ZN2cv6imreadERKNS_6StringEi'
//home/username/local/opencv-3.4.7/lib/libopencv_imgcodecs.so.3.4: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
username@HOME:~/wk/exp6/scc$ g++ main.cpp -o a.out -I/home/username/local/opencv-3.4.7/include -L/home/username/local/opencv-3.4.7/lib/ -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs
- Makefile
CMake
http://blog.csdn.net/lj402159806/article/details/76408597 (安裝)
http://www.reibang.com/p/c042abb42133 (使用教程)
sudo apt-get install cmake
(版本匹配很重要,新版的可以直接從源碼編譯安裝也很簡(jiǎn)單覆致。對(duì)于特定的開(kāi)發(fā)項(xiàng)目可能需要特定的cmake版本肠骆,建立一個(gè)軟連接將/usr/local/bin/目錄下就行了)
編寫(xiě)CMakeLists.txt
cmake_minimum_required (VERSION 2.6) //cmake版本配置
project () //項(xiàng)目信息描述,僅僅是個(gè)描述而已,好像沒(méi)任何用處
add_executable (exe src1 src2 ...) //最終的可執(zhí)行文件, + 使用哪些源文件進(jìn)行生成
add_library (lib [STATIC|SHARED] src) //生成庫(kù)文件
target_link_libraries (whichexe whichlib <debug|optimal> lib2 <>...) // 指定將某個(gè)庫(kù)鏈接到對(duì)應(yīng)的可執(zhí)行文件中
add_subdirectory ()//添加子文件夾隘梨,其中的CMakeList也會(huì)遞歸的處理
aux_source_directory(realdirname NAME) //將所有dir下的所有文件名 添加到變量cmake變量中程癌,引用變量:${NAME}
set(YOURNAME main.cpp pf.cpp) //定義cmake變量
link_directories(dir1 dir2) //添加鏈接庫(kù)的搜索路徑
include_directories([BEFORE|AFTER] dir1 dir2 ) //添加頭文件的搜索路徑,before和after用來(lái)控制其與當(dāng)前路徑之間的前后關(guān)系
add_definitions (-DDEBUG)//添加編譯器編譯命令行參數(shù)
執(zhí)行cmake .
之后會(huì)自動(dòng)檢測(cè)系統(tǒng)的各種編譯器 生成對(duì)應(yīng)的編譯工程文件(通過(guò)源碼生成編譯器需要的整個(gè)工程文件如vs的sln和vcxproj工程文件)轴猎。
整個(gè)過(guò)程的錯(cuò)誤信息會(huì)記錄到cmake的log文件中席楚。
編譯使用了OpenCV的工程:CMakeList.txt
cmake_minimum_required(VERSION 2.8)
project(opencv)
find_package(OpenCV REQUIRED)
add_executable(opencv opencv.cpp)
target_link_libraries(opencv ${OpenCV_LIBS})
vscode config
https://www.cnblogs.com/YiYA-blog/p/10082635.html
./.vscode/launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "g++",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"command": "g++",
"args": [
"-g", "-std=c++11", "${file}", "-o", "${fileBasenameNoExtension}",//"main.cpp", "text.cpp","-o","a.out",
"-I", "/home/username/local/opencv-3.4.7/include",
"-I", "/home/username/local/opencv-3.4.7/include/opencv",
"-I", "/home/username/local/opencv-3.4.7/opencv2",
"-L", "/home/username/local/opencv-3.4.7/lib",
"-l", "opencv_core",
"-l", "opencv_imgproc",
"-l", "opencv_imgcodecs",
"-l", "opencv_video",
"-l", "opencv_ml",
"-l", "opencv_highgui",
"-l", "opencv_objdetect",
"-l", "opencv_flann",
"-l", "opencv_imgcodecs",
"-l", "opencv_photo",
"-l", "opencv_videoio"
],// g++編譯命令參數(shù)
"problemMatcher":{
"owner": "cpp",
"fileLocation":[
"relative",
"${workspaceFolder}"
],
"pattern":[
{
"regexp": "^([^\\\\s].*)\\\\((\\\\d+,\\\\d+)\\\\):\\\\s*(.*)$",
"file": 1,
"location": 2,
"message": 3
}
]
},
"group": {
"kind": "build",
"isDefault": true
}
}
For vscode
c_cpp_properties.json:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/home/username/local/opencv-3.4.7/include",
"/home/username/local/opencv-3.4.7/include/opencv2",
"/home/username/local/opencv-3.4.7/include/opencv"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}
ctrl+shift+B compile
https://blog.csdn.net/u014267567/article/details/53842915 OpenCV中實(shí)現(xiàn)MATLAB中的conv2函數(shù)
gcc
gcc 命令幾個(gè)重要選項(xiàng):
-shared 該選項(xiàng)指定生成動(dòng)態(tài)連接庫(kù)(讓連接器生成T類型的導(dǎo)出符號(hào)表,有時(shí)候也生成弱連接W類型的導(dǎo)出符號(hào)税稼,不用該標(biāo)志外部程序無(wú)法連接烦秩。相當(dāng)于一個(gè)可執(zhí)行文件)。
-fPIC:表示編譯為位置獨(dú)立的代碼郎仆,不用此選項(xiàng)的話編譯后的代碼是位置相關(guān)的所以動(dòng)態(tài)載入時(shí)是通過(guò)代碼拷貝的方式來(lái)滿足不同進(jìn)程的需要只祠,而不能達(dá)到真正代碼段共享的目的。
-L.:表示要連接的庫(kù)在當(dāng)前目錄中扰肌。
-lxx:編譯器查找動(dòng)態(tài)連接庫(kù)時(shí)有隱含的命名規(guī)則抛寝,即在給出的名字前面加上lib,后面加上.so來(lái)確定庫(kù)的名稱(libxx.so)
修改gcc版本:
- 安裝完新的gcc之后曙旭,一般是放在/usr/bin/gcc-4.9 附帶版本號(hào)
- 系統(tǒng)真正使用的是/usr/bin/gcc這個(gè)軟鏈接盗舰,所以安裝完成之只需要將軟鏈接連接到新的gcc上就可以了。
http://blog.csdn.net/autoliuweijie/article/details/50950300
http://blog.csdn.net/keith_bb/article/details/52864851
http://blog.csdn.net/luotuo44/article/details/24836901 ()
http://blog.csdn.net/autoliuweijie/article/details/50950300
-w 不生成任何警告信息
-Wall生成所有警告信息
-o指定生成的文件名(-E,-S,-c說(shuō)明生成什么樣的文件)
-O0~3編譯優(yōu)化
-g生成調(diào)試信息
-E只運(yùn)行預(yù)編譯(處理include頭文件等)不生成文件
-S翻譯成匯編語(yǔ)言
-c生成.o二進(jìn)制目標(biāo)文件
-I添加額外的頭文件搜搜路徑桂躏,否則系統(tǒng)僅僅在標(biāo)準(zhǔn)目錄中搜索(先再當(dāng)前目錄查找then系統(tǒng)目錄钻趋,定義了-I則先在I目錄查找then當(dāng)前then系統(tǒng))
-idirafter(-I的目錄放到后面)
-include+dir/xxx.h 直接include一個(gè)頭文件
-L添加額外的庫(kù)搜索路徑
-l連接庫(kù)(需要放到最后,且要省去前面的lib和后面的.a)
-static使用靜態(tài)連接庫(kù)生成
-shared使用動(dòng)態(tài)鏈接庫(kù)連接
-MM生成文件關(guān)聯(lián)信息
gcc 后面是需要編譯的文件列表(修飾-E,-S,-c),生成控制-o剂习,優(yōu)化-O蛮位,-W,-g鳞绕,額外的頭文件搜索目錄-I失仁,庫(kù)目錄-L,庫(kù)-l
gcc -E main.c > preprocess.txt 預(yù)處理
gcc -S main.c 預(yù)處理+編譯生成匯編文件
gcc -c main.c 生成.o文件
gcc -o a.out main.cpp hello.cpp -lopencv
g++ pkg-config opencv --cflags
opencv.cpp -o opencv pkg-config opencv --libs
gcc main.c pkg-config --cflags --libs gtk+-2.0
-o main
http://blog.csdn.net/luotuo44/article/details/16970841 (c語(yǔ)言編譯詳解)
編譯成庫(kù)文件:
靜態(tài)庫(kù):
ar -cr libpalm.a add.o sub.o (將目標(biāo)文件鏈接成一個(gè)靜態(tài)庫(kù)文件libxxx.a)
ar -t libpalm.a 查看靜態(tài)庫(kù)libpalm.a中包含的.o目標(biāo)文件
動(dòng)態(tài)庫(kù):
http://www.cnitblog.com/windone0109/archive/2008/04/23/42653.html
gcc -c pos_conf.c
gcc -shared -fPCI -o libpos.so pos_conf.o
gcc -o pos main.c -L. -lpos 常用的寫(xiě)到默認(rèn)路徑们何,不常用的編譯時(shí)通過(guò)參數(shù)指明
/etc/ld.so.conf
ldconfig
LD_LIBRARY_PATH
使用c++11:
gcc -o out main.cpp -std=c++11
References:
https://blog.csdn.net/s15810751918/article/details/107705387