开发环境

编辑器

Visual Studio Code

VIM

编译

img

编译器

gcc/g++
sudo apt-get install -y g++
sudo apt-get install -y build-essential
g++ -std=c++17 -Wall -Wextra -Wpedantic input.cpp -o output

-g:compile for debugging; avoid optimization level -O1,-O2,-O3, use -O0, -Og instead.

-D MACRO: 定义宏;

-I path:添加搜索路径;

-l lib:添加外部库;

-L path:外部库目录;

The use of gcc does not add the C++ library. g++ is a program that calls gcc and automatically specifies linking against the C++ library.

GCC的gcc和g++区别

clang/clang++
sudo apt-get install -y clang
sudo apt-get install -y build-essential

在规模较大的项目中,往往需要多个源程序文件,每个源程序文件称为一个编译单元。一个类的声明必须出现在所有使用该类的编译单元中。

链接

跨目标文件查找符号(变量、函数)并进行关联。

默认的程序入口函数为main,可通过连接器选项指定。

static声明的函数只会在当前文件作用域被调用,因此当此函数未在该文件中被调用时,链接器不会尝试去链接该函数内部调用的其他函数。

静态链接

外部库的路径

包含目录$INCLUDE:头文件的搜索路径;

库目录$LIB:库文件的搜索路径;

Visual Studio: 在c++项目属性中配置Visual C++目录。

链接到外部库

为连接器指定库文件。

Visual Studio:在c++项目属性中配置“连接器/附加依赖项”,在给定库目录路径后,只需给出文件名即可。在同一解决方案下的库,可使用“添加/引用”的方式导入依赖库。

静态链接可以执行链接优化。

动态链接

xxxdll.lib

xxx.dll文件放置在可执行文件所在目录(或设置库目录)。

工具链

make

先声明最后的输出文件(可执行文件)的构建命令,然后分别声明依赖文件的构建命令。make根据依赖关系决定是否继续查找后续目标,然后反向执行命令。

prog: main.o dbl.o
	g++ main.o dbl.o -o prog
main.o : main.cpp
	g++ -c main.cpp
dbl.o : dbl.cpp
	g++ -c dbl.cpp  # tab for indentation
clean:
	rm -rf *.o prog
install:
   install prog /usr/local/bin/

仅编译更新过的源文件、目标文件。

Makefile Tutorial By Example

设置编译器选项(CFLAGS)
 ./configure CFLAGS='-g -O2 -w' CXXFLAGS='-g -O2 -w'
 make CFLAGS='-g -O2 -w' CXXFLAGS='-g -O2 -w'
编译项目
make & make install # building project and install the software
make clean			# cleaning the building output
make uninstall		# uninstall the software from the configured location

cmake

https://cliutils.gitlab.io/modern-cmake。

sudo apt install cmake

https://https//github.com/lefticus/cpp_starter_project。

# Makefile
camke_minimum_required(VERSION 3.5)
project (HelloWorld)
set (CMAKE_CXX_FLAGS "$CMAKE_CXX_FLAGS -Wall -Werror -std=c++11")
set (source_dir "$PROJECT_SOURCE_DIR/src/")
file (GLOB source_files "$source_dir/*.cpp")
add_executable (HelloWorld $(source_files))
Cmake -G "Codelite - Unix Makefiles" -DCMKAE_BUILD_TYPE=Debug

MSBuild

build2

QMake

IDE

Visual Studio

  • Visual Studio Free Community Edition

    VisualAssist

    VC++项目属性配置

    目录配置:使用宏(例如$(SolutionDir))配置输出目录。

    配置类型:可执行文件(.exe)、静态库(.lib)、动态库(.dll)……。

    语言标准:

    C++/CLI属性

  • Qt Creator

  • CodeLite

    Code Completion
    • C++ code completion: powerful and amazingly fast code completion based on our in-house parser (supports C++11 auto keyword, templates, inheritance etc)
    • clang based code completion for C++ projects - based on the CLANG/LLVM project - this feature provides a compiler level code completion
    • Parse and display doxygen comments in the code completion box (as a separated floating window)
    Compilers
    • Generic support for compilers with built-in support for GCC/clang/VC++
    • Display errors as code annotations or as tooltip in the editor window
    • Errors are clickable via the Build tab
    LLDB Support
    GDB Support
    • Watches table - add persistent watches with a single click
    • Quick Watch - right click on a variable to expand it
    • Locals tree - automatically displays the current stack variables (also displays *this if available)
    • Threads view - contains a list of currently-running threads; switch between them with a single click
    • Breakpoint management - disable, enable, conditional, temporary
    • Automatic tree like tooltips
    • Memory view
    • Tree View tooltips (auto de-references pointers)
    • Allow modifying tooltips
    Refactoring
    • Rename symbol, local symbol
    • Rename file (will also change all #include in the code)
    • Generate setters / getters
    • Implement inherited virtual functions
    • Implement inherited pure-virtual functions
    • Easily change function signature to match its header / implementation counterpart
    • Move functions implementation to another source file
    • Implement all unimplemented methods
    • Implement method
    Source Control Plugins

标准库

标准模板库(STL)。

Microsoft 基础类 (MFC) 通过 Win32 提供面向 C++ 对象的包装,以便实现本机桌面应用程序的快速开发。 活动模板库 (ATL, ActiveX Template Library) 是一个包装库,它简化了 COM 开发,广泛用于创建 ActiveX 控件。

工具集

命令行工具:

工具命令说明
hexdumphexdump -C file查看文件的十六进制编码
readelfreadelf -aW file查看ELF二进制文件结构

Red Hat Developer Toolset

Red Hat Developer Toolset does not replace the default system tools.

Enable Red Hat Developer Toolset by using Red Hat Subscription Management.

yum install devtoolset-10 # all development, debugging, and performance monitoring tools
# devtoolset-10-toolchain
# devtoolset-10-perftools

使用指定的工具

scl enable devtoolset-10 'gcc -o output_file source_file...'
scl enable devtoolset-10 'bash' # 使开发工具集中的执行程序成为默认

Developing C and C++ applications in RHEL 8 Red Hat Enterprise Linux 8 | Red Hat Customer Portal