gcc,g++,gdb,和動態(tài)庫的配置

1

先說一些gcc 看不到的命令

-I xxxxx/xxx/

-I(小寫是i),是指定頭文件查找的目錄。沒有指定默認,則去C_INCLUDE_PATH或者CPLUS_INCLUDE_PATH環(huán)境變量去找,然后去C/C++特定的目錄去找。

-lxxx

,-l(大寫是L),指定要要鏈接的動態(tài)庫名。你的動態(tài)庫可能是libxxx.so,那么這里只要-l跟上xxx極可能沒去掉lib和.so,他就知道你要找的是libxxx.so.-l和xxx中間沒有空格

-L xxxx/xxxx

是指定程序鏈接動態(tài)庫時要找目錄,首先找xxxx/xxxx,然后環(huán)境變量LD_LIBRARY_PATH去找,然后是/lib和/usr/lib兩個目錄找動態(tài)庫。-L僅在編譯時有用。不會將改目錄寫到可執(zhí)行文件中去。


拿test.c和test.h來打比方,
如果按照下面的方法打包動態(tài)庫:

gcc -c  test.c -o test.o -fPIC
gcc -share -o libtest.so test.o  

最后生成了一個動態(tài)庫libtest.so的動態(tài)庫。
如果頭文件和 動態(tài)庫,都指定在mian.c同一個目錄,則編譯代碼如下:

gcc main.c -I . -ltest -L . -o mian.out

但是執(zhí)行mian.out 時卻報錯:

./main.out: error while loading shared libraries: libtest.so: cannot open shared object file: No such file or directory

卻找不到libtest.so。
原因是-L只在編譯階段起作用。程序還是會去LD_LIBRARY_PATH和配置文件去找,然后去系統(tǒng)庫目錄找(/lib /usr/lib)。結(jié)果都沒找到。
所以你只要把他放在/lib /usr/lib即可了?好麻煩啊。我明明想要的結(jié)果是跟隨我的可執(zhí)行文在同一個目錄。你給別人程序時,還要告訴人家放到哪里,還要解決權(quán)限問題。這不科學,是不是這設(shè)計的太辣雞了。于是乎,又一個gcc 參數(shù)出來了。

-wl,-rpath=路徑

這個參數(shù)和-I(小寫是i)一樣,告訴程序首先去這個目錄找這個動態(tài)庫。只要在可執(zhí)行程序在編譯階段加上這個參數(shù),這個路徑會編譯到可執(zhí)行程序中去。所以上面的編譯main.c改成這樣既可:

gcc main.c -I . -wl,-rpath=.  -ltest    -L . -o mian.out

這樣,libtest.so和main.out 放在同一目錄就可以了。給別人時也很方便了。


2

這里探討gcc到底可以不可以編譯C++.
當我用gcc編譯cpp文件時卻報錯:


/usr/bin/ld: /tmp/ccLgjUmA.o: in function `main':
main.cpp:(.text+0x12): undefined reference to `std::cout'
/usr/bin/ld: main.cpp:(.text+0x17): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: main.cpp:(.text+0x21): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/usr/bin/ld: main.cpp:(.text+0x2c): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/bin/ld: /tmp/ccLgjUmA.o: in function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x60): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: main.cpp:(.text+0x75): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

上面錯誤大概是找不到C++的庫。
百度了很久,都說智能換成g++.最后發(fā)現(xiàn)gcc照樣可以編譯cpp文件。
操作如下:

 gcc main.cpp -lstdc++ -o main.out

只要告訴gcc 這個動態(tài)庫叫啥,像上面的-lstdc++.就是告訴gcc這個動態(tài)庫叫l(wèi)ibstdc++.so.他就會去系統(tǒng)庫里面找了。找到之后就可以順利編譯通過。
gcc g++都可以編譯C++。g++不要告訴g++要鏈接的動態(tài)庫。而gcc就麻煩一些,要用-l(大寫是L)告訴gcc這個庫叫啥。


3.

根據(jù)上面的東西。又百度了一些。配置了在vscode中使用gcc和g++調(diào)試程序。

gcc的配置:

launch.json

{
    // 使用 IntelliSense 了解相關(guān)屬性。
    // 懸停以查看現(xiàn)有屬性的描述。
    // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "gcc - 生成和調(diào)試活動文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "為 gdb 啟用整齊打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

task.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "gcc ",
            "args": ["-lstdc++","-std=c++11","-o","${fileBasenameNoExtension}.out"]
        }
    ]
}

簡單說明下:args就是你編譯時的gcc 參數(shù)。這里指定依賴的動態(tài)庫:-lstdc++

 sudo find / -name libstdc++.so

看看這個庫在哪:

/usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.so
g++ 的配置

launch.json

{
    // 使用 IntelliSense 了解相關(guān)屬性。
    // 懸停以查看現(xiàn)有屬性的描述。
    // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - 生成和調(diào)試活動文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "為 gdb 啟用整齊打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "build",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

task.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": ["-g","${file}","-std=c++11","-o","${fileBasenameNoExtension}.out"]
        }
    ]
}

兩者配置大同小異,就在簡歷task是指定的編譯參數(shù)不一樣。
用gdb在vscode控制臺調(diào)試時,要在前面加上-exec:

-exec p 10

task.json中的tasks是個多數(shù)組,支持多任務(wù)的。而且支持任務(wù)之間的依賴關(guān)系。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build2",
            "type": "shell",
            "command": "ls",
            "args": ["-l"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "build1",
            "type": "shell",
            "command": "rm",
            "args": ["-rf","${fileBasenameNoExtension}.out"],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn":["build2"]
        },
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": ["-g","${file}","-std=c++11","-o","${fileBasenameNoExtension}.out"],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn":["build1"]
        }
    ]

}

編譯任務(wù)build依賴刪除任務(wù)'build1',而刪除任務(wù)build1又依賴打印目錄任務(wù)build2.
很舒服

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容