OpenCV, g++, vscode

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;
}
  1. 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 
  1. 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
  1. 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 (\star)
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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末萄焦,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子冤竹,更是在濱河造成了極大的恐慌拂封,老刑警劉巖,帶你破解...
    沈念sama閱讀 212,454評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件贴见,死亡現(xiàn)場(chǎng)離奇詭異烘苹,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)片部,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門镣衡,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)霜定,“玉大人,你說(shuō)我怎么就攤上這事廊鸥⊥疲” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 157,921評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵惰说,是天一觀的道長(zhǎng)磨德。 經(jīng)常有香客問(wèn)我,道長(zhǎng)吆视,這世上最難降的妖魔是什么典挑? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,648評(píng)論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮啦吧,結(jié)果婚禮上您觉,老公的妹妹穿的比我還像新娘。我一直安慰自己授滓,他們只是感情好琳水,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,770評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著般堆,像睡著了一般在孝。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上淮摔,一...
    開(kāi)封第一講書(shū)人閱讀 49,950評(píng)論 1 291
  • 那天私沮,我揣著相機(jī)與錄音,去河邊找鬼噩咪。 笑死顾彰,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的胃碾。 我是一名探鬼主播,決...
    沈念sama閱讀 39,090評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼筋搏,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼仆百!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起奔脐,我...
    開(kāi)封第一講書(shū)人閱讀 37,817評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤俄周,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后髓迎,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體峦朗,經(jīng)...
    沈念sama閱讀 44,275評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,592評(píng)論 2 327
  • 正文 我和宋清朗相戀三年排龄,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了波势。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,724評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖尺铣,靈堂內(nèi)的尸體忽然破棺而出拴曲,到底是詐尸還是另有隱情,我是刑警寧澤凛忿,帶...
    沈念sama閱讀 34,409評(píng)論 4 333
  • 正文 年R本政府宣布澈灼,位于F島的核電站,受9級(jí)特大地震影響店溢,放射性物質(zhì)發(fā)生泄漏叁熔。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,052評(píng)論 3 316
  • 文/蒙蒙 一床牧、第九天 我趴在偏房一處隱蔽的房頂上張望荣回。 院中可真熱鬧,春花似錦叠赦、人聲如沸驹马。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,815評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)糯累。三九已至,卻和暖如春册踩,著一層夾襖步出監(jiān)牢的瞬間泳姐,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,043評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工暂吉, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留胖秒,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,503評(píng)論 2 361
  • 正文 我出身青樓慕的,卻偏偏與公主長(zhǎng)得像阎肝,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子肮街,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,627評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容