Skip to content

The-C

3 posts with the tag “The-C”

gcc-g++ parameters1. Compilation steps

  1. Preprocess and generate .i file 2. Convert the preprocessed files into assembly language and generate .s files 3. From assembly to object code, generate .o file 4. Connect the target code and generate an executable program2. Common parameters
  2. -c
    Only activate preprocessing, compilation, and assembly, do not connect, and generate obj (*.o) files
  3. -o
    Specify target file name
  4. -S
    Preprocessing + compilation, no assembly connection
  5. -E
    Only preprocessing
  6. -O0/-O1/-O2/-O3
    There are 4 levels of compiler optimization, -O0 has no optimization, -O1 is the default value, and -O3 has the highest optimization level.
  7. -g
    Generate debugging information at compile time
  8. -ggdb
    Generate debugging information that gdb can use when possible
  9. -w
    Does not generate any warning message
  10. -Wall
    Generate all warning messages
  11. -Wl,-options1,-options2
    Connection options
  12. -LLIBRARY
    Search the specified function library when connecting
  13. -IDIRECTORY (this is a capital i)
    Specify additional header file search paths
  14. -nostartfiles -nostdlib
    It is necessary to run bare metal without Linux. -nostartfiles does not connect to the system standard startup files. -nostdlib includes -nostartfiles and standard libraries.
  15. -static
    Static linking shared library files
  16. -shared
    Connect multiple target files to generate a shared library file ending in .a

VSCode C/CPP Environment

1. Install MinGWFor details, please refer to [C++ environment settings]

Section titled “1. Install MinGWFor details, please refer to [C++ environment settings]”

2. Configure VSCodeCreate a.vscodefolder under the source code folder, which containsc_cpp_properties.json,launch.json,tasks.json. For the specific meaning of each file, please refer to [VScode tasks.json and launch.json settings], [VSCode predefined variables]

Section titled “2. Configure VSCodeCreate a.vscodefolder under the source code folder, which containsc_cpp_properties.json,launch.json,tasks.json. For the specific meaning of each file, please refer to [VScode tasks.json and launch.json settings], [VSCode predefined variables]”

(1).c_cpp_properties.jsonNeed to change MinGW path to actual path

Section titled “(1).c_cpp_properties.jsonNeed to change MinGW path to actual path”
{
"configurations": [
{
"name": "RHEL7.2->Linux",
"includePath": [
"${workspaceFolder}/**",
"/opt/scadacom/current/**"
],
"forcedInclude": [],
"defines": [],
"compilerPath": "/usr/bin/g++",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [ //Release
"-std=c++11",
"-w",
"-O2",
"-pipe",
"-m64",
"-D_REENTRANT",
"-DQT_SHARED",
"-DQT_NO_DEBUG_OUTPUT",
"-DQT_NO_DEBUG",
"-DQT_GUI_LIB",
"-DQT_NETWORK_LIB",
"-DQT_CORE_LIB",
"-DQT_SHARED"
]
},
{
"name": "RHEL7.2-WindowsLocal",
"includePath": [
"${workspaceFolder}/**",
"~/Documents/Codes/include/RHEL7.2S5.8.10/**",
"C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/x86_64-w64-mingw32/include"
],
"forcedInclude": [],
"defines": [],
"compilerPath": "C:/ProgramData/chocolatey/bin/g++.exe",
"cStandard": "c11",
"cppStandard": "c++11",
"intelliSenseMode": "linux-gcc-x64",
"compilerArgs": [ //Release
"-std=c++11",
"-w",
"-O2",
"-pipe",
"-m64",
"-D_REENTRANT",
"-DQT_SHARED",
"-DQT_NO_DEBUG_OUTPUT",
"-DQT_NO_DEBUG",
"-DQT_GUI_LIB",
"-DQT_NETWORK_LIB",
"-DQT_CORE_LIB",
"-DQT_SHARED"
],
"mergeConfigurations": false,
"browse": {
"path": [
"${workspaceFolder}/**",
"~/Documents/Codes/include/RHEL7.2S5.8.10/**"
],
"limitSymbolsToIncludedHeaders": true
}
}
],
"version": 4
}

(2).launch.jsonNeed to change MinGW path to actual path

Section titled “(2).launch.jsonNeed to change MinGW path to actual path”
{
"version": "0.2.0",
"configurations": [
{
"name": "QT-CPPGDB",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/wg_modbus_ats",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/debug/",
"environment": [],
//"symbolSearchPath": "C:\\Symbols",
"externalConsole": false,
"linux": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"windows": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"osx": {
"miDebuggerPath": "/Applications/Xcode.app/Contents/Developer/usr/bin/lldb-mi",
"MIMode": "lldb"
},
// "logging": {
// "moduleLoad": false,
// "trace": true
// },
"visualizerFile": "my.natvis"
},
{
"name": "CMake-CPPGDB",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/${workspaceFolderBasename}",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}/build",
"environment": [],
//"symbolSearchPath": "C:\\Symbols",
"externalConsole": false,
"linux": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"windows": {
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
"osx": {
"miDebuggerPath": "/Applications/Xcode.app/Contents/Developer/usr/bin/lldb-mi",
"MIMode": "lldb"
},
// "logging": {
// "moduleLoad": false,
// "trace": true
// },
"visualizerFile": "my.natvis"
}
]
}

(3). tasks.jsonYou can copy the file directly

Section titled “(3). tasks.jsonYou can copy the file directly”
{
"version": "2.0.0",
// "windows": {
// "options": {
// "shell": {
// "executable": "bash.exe",
// "args": ["-c"]
// }
// }
// },
"tasks": [
{
"label": "Qmake",
"type": "shell",
"command": "/usr/local/Trolltech/Qt-4.8.4/bin/qmake",
"args": [],
"group": "build",
"presentation": {
"reveal": "silent",
"clear": true
}
},
{
"label": "Clean",
"type": "shell",
"command": "make",
"args": [
"clean"
],
"group": "build",
"presentation": {
"reveal": "silent",
"clear": true
}
},
{
"label": "MakeCompile",
"type": "shell",
"dependsOrder": "sequence",
"dependsOn": [
"Clean",
"Qmake"
],
"command": "/usr/bin/make",
"args": [
],
"group": "build",
"presentation": {
// "reveal": "silent",
"clear": true
}
},
]
}

[2]. https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables

[3]. https://zhuanlan.zhihu.com/p/92175757

[4]. https://www.cnblogs.com/bpf-1024/p/11597000.html

[5]. https://zhuanlan.zhihu.com/p/87864677

[C++ environment setup]: https://www.runoob.com/cplusplus/cpp-environment-setup.html[VSCode predefined variables]: https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables[Settings of VScode tasks.json and launch.json]: https://zhuanlan.zhihu.com/p/92175757

C Learning Notes

gcc -std=c99 filename.c

c99, c1x, and c11 are examples of available standards.

gcc -o output_file filename.c
gcc filename.c -I/usr/local/include
gcc filename.c -lssl

[1]. gcc-g++参数

[2]. Makefile-初探

[3]. Qmake-Pro