C/C++
Est. read time: 3 minutes | Last updated: November 07, 2024 by John Gentile
Contents
The Language
C++ has language facilities similar to the C language such as being a procedural programming language but is vastly different in many ways.
CppCon has great talks on new C++ features as well.
The Basics
The proverbial “Hello, World!” implementation in C and C++ introduces a few basic parts of the language.
C:
#include <stdio.h>
main()
{
printf("Hello, World!\n");
}
C++:
#include <iostream>
int main()
{
std::cout << "Hello, World!\n";
}
Tools
Building C++ Programs
C++ must be compiled from source files into object files and then linked into an executable file.
C++ is a statically typed language in that every entity (i.e. object, name, value, etc.) must be known to the compiler at compile-time.
Compilers
GCC/clang Compiler Flags
Also see gentoo linux’s notes on GCC optimization:
-g
: compiler and linker should generate, and retain, source-level debugging/symbol information in the executable itself. Helpful in debugging builds.-O2/O3
: for optimization flags added. Note that you should profile both, as on sometimes -O3 is slower code than -O2.-march=native
: compiles machine-specific instructions for the host CPU running the compile. Can unlock a lot of performance on hosts with SIMD units.
GNU cc (gcc)
gcc
is a common compiler for the C language (as well as C++ and Objective C). Debug information and optimizations can also be utilized.
LLVM/Clang
Online Compilers
- ideone: Online compiler and debugging tool that supports over 60 code languages
- godbolt: Compiler explorer to examine machine code output for various compile chains supporting a couple code languages (C++, D, Rust, and Go)
Build Systems
GNU Make
When a makefile exists for a given directory/project (under the form makefile
, Makefile
) simply running the shell command $ make
executes commands and instructions given in the makefile.
CMake
Some other great CMake documentation and repos:
Bazel
Testing
- GoogleTest: C++ test and mocking framework.
Code Analysis Tools
- Valgrind: instrumentation framework to detect memory management and threading bugs.
- Facebook Infer: static analysis of C/C++/Objective-C and Java code.
- clangd Language Server: adds code completion, compile errors, go-to-definition and other features to tools that use the Language Server Protocol (LSP).
- clang-tidy: linting tool for style violations, interface misuse or bugs that can be deduced via static analysis.
- cpplint: similar to
clang-tidy
, but specifically for Google’s C/C++ Style Guide.
- cpplint: similar to
- clang-format: a tool to format C/C++ (and other similar language) code.
- Uncrustify: similar tool for reformatting.
- oclint: static source code analysis tool for C/C++ and Objective-C.
- cppcheck: another static analysis tool for C/C++ code.
- clang-tidy: linting tool for style violations, interface misuse or bugs that can be deduced via static analysis.
Other Tools
- cdecl: C gibberish ↔ English translator.
- Doxygen: generates documentation from inline comments in source code.
C++ Libraries
References
C
- The C Programming Language 2nd Edition by Brian W. Kernighan and Dennis M. Ritchie
- Practical C Programming, 3rd Edition by Steve Oualline
- Modern C
C++
- The Standard: website for the current and draft ISO C++ standards
- The C++ Annotations by Frank B. Brokken
- Google C++ Style Guide
- C++ High Performance, Second Edition
- Microsoft C/C++ Documentation
Repos
- facebook/folly: An open-source C++ library developed and used at Facebook.
- abseil / abseil.io
- TheAlgorithms/C-Plus-Plus - GitHub
- actor-framework/actor-framework: An Open Source Implementation of the Actor Model in C++
- nlohmann/json - JSON for Modern C++