GCC编译范例
C++ GNU编译器GCC的主要参数说明;在vscode上编译文件的范例。

GCC编译范例


$ g++
-Wall -Wextra 提示警告
-Wno-unused-variable 不显示警告
-Wno-unused-parameter 不显示警告
-std=c++20 使用c++20标准
-g 仅调试时使用,使用后生产执行文件体积巨大。标记additional symbolic debuggging information for use with gdb debugger.
-Og 优化
-f 用于启用或禁用特定的编译器功能或特性。如-fdiagnostics-color=always 反馈结果彩色显示

zzz.cpp 被编译的cpp源文件,多个文件空格隔开

-o zzz 指定生成的可执行文件的名字

注解:

  • -Wno-xxxx 不显示xxxx警告
  • -std=c++20 需要GCC 10及其以上版本。也可 std=gnu++20 enable GNU extensions in addition to C++20 features
  • -O0(没有优化)、-O1(优化少)、-Og(debug优化)、-O2(优化中)、-Os(size小)、-O3(优化多)、-Ofast(速度快)

其他:
-D 定义预处理器宏。
-c 只编译,不linking

Linking

-L 加链接库的path。如-L/home/uussrr/lib/
-pthread 链接多线程库
-l 链接library名。如-lsqlite3-lncurses,分别链接libsqlite.solibncurses.so
注意: 确保so文件的路径在/etc/ld.so.conf中存在,如不存在需添加conf文件,并 $ sudo ldconfig
注意:If one library depends on another, put the dependent library BEFORE the independent library!
如:-lchild-lib -lbase-lib

常用链接库

  • -lsqlite3 sqlite3轻量数据库。(libsqlite3-dev sqlite3)
  • -lncurses 终端字符处理库。(libncurses-dev)
  • -lgsl -lgslcblas GNU开源科学计算库,并使用内置的BLAS子库。(libgsl-dev)
    • -lgsl -lcblas -latlas 使用内置的BLAS接口并用atlas子库(比gslcblas速度快)。(libgsl-dev libatlas-base-dev)

备注:BLAS 基本线性代数子程序。高性能计算常用。
备注:GSL的替代,MKL 英特尔高级数学库,比GSL优化好。


VSCode C++ 编译

项目文件夹中的.vscode文件夹是配置文件夹,需创建如下3个文件:

  • tasks.json: 编译设置文件。告诉VSCode如何生成(编译)程序
  • launch.json: debugging(调试器)设置文件
  • c_cpp_properties.json: 编译器路径和 IntelliSense 设置

(一)tasks.json配置

通过菜单 Terminal > Configure Default Build Task 生成模板,在此基础上修改。

(官网:)https://code.visualstudio.com/docs/editor/variables-reference

  • ${workspaceFolder} - the path of the folder opened in VS Code
  • ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
  • ${file} - the current opened file
  • ${fileWorkspaceFolder} - the current opened file’s workspace folder
  • ${relativeFile} - the current opened file relative to workspaceFolder
  • ${relativeFileDirname} - the current opened file’s dirname relative to workspaceFolder
  • ${fileBasename} - the current opened file’s basename
  • ${fileBasenameNoExtension} - the current opened file’s basename with no file extension
  • ${fileDirname} - the current opened file’s dirname
  • ${fileExtname} - the current opened file’s extension
  • ${cwd} - the task runner’s current working directory on startup
  • ${lineNumber} - the current selected line number in the active file
  • ${selectedText} - the current selected text in the active file
  • ${execPath} - the path to the running VS Code executable
  • ${defaultBuildTask} - the name of the default build task
  • ${pathSeparator} - the character used by the operating system to separate components in file paths

范例文件:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "cppbuild",
			"label": "C/C++: g++ build active file",
			"command": "/usr/bin/g++",
			"args": [
				"-Wall",
                "-Wextra",
                // "-Wno-unused-variable",
                "-Wno-unused-parameter",
                "-std=gnu++20",
                // "-g", // 仅调试用。用了体积巨大
                "-O3",
                "-fdiagnostics-color=always",

				"${workspaceFolder}/aa.cpp",
                "${workspaceFolder}/bb.cpp",
                "${workspaceFolder}/cc.cpp",
                "${workspaceFolder}/dd.cpp",
                "${workspaceFolder}/ee.cpp",
                "${workspaceFolder}/ff.cpp",
                "${workspaceFolder}/gg.cpp",
                "${workspaceFolder}/main.cpp",
                // "${workspaceFolder}/*.cpp",

				"-o",
				"${workspaceFolder}/${workspaceRootFolderName}",

				"-L/home/uusseerr/api/",
				"-pthread",
				"-lgsl",
				"-lcblas", // 比gslcblas快
				"-latlas", // 比gslcblas快
				// "-lgslcblas",
                "-lsqlite3",
                "-lncursesw",
				"-lxxxapi",
				"-lyyyapi",
				"-DSQLITE_THREADSAFE=0" // sqlite3 单线程模式
			],
			"options": {
				"cwd": "${fileDirname}"
			},
			"problemMatcher": [
				"$gcc"
			],
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"detail": "compiler: /usr/bin/g++"
		}
	]
}

(二) launch.json配置

通过菜单 Run > Add Configuration 生成模板,C++ gdb Launch ,在此基础上修改。

需要手动修改:

  • program:启动文件的地址
  • cwd:程序启动调试的目录

(官网:)https://code.visualstudio.com/docs/editor/debugging

范例文件:

{
    // 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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description":  "Set Disassembly Flavor to Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ],
/*          "preLaunchTask": "C/C++: g++ build active file"  // 运行前,先编译。  */ 
        }
    ]
}

(三) c_cpp_properties.json配置

按快捷键 Ctrl+Shift+P 打开命令面板,选择 C/C++: Edit Configurations(UI),生成模板,在此基础上修改。

范例文件:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "${workspaceFolder}/../"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "gnu17",
            "cppStandard": "gnu++20",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

最后修改于 2024-02-24