Developing C and C++ applications in RHEL 9 Red Hat Enterprise Linux 9 Setting up a developer workstation, and developing and debugging C and C++ applications in Red Hat Enterprise Linux 9 Red Hat Customer Content Services

Commands:
subscription-manager repos --enable rhel-9-for-USD(uname -i)-baseos-debug-rpms subscription-manager repos --enable rhel-9-for-USD(uname -i)-baseos-source-rpms subscription-manager repos --enable rhel-9-for-USD(uname -i)-appstream-debug-rpms subscription-manager repos --enable rhel-9-for-USD(uname -i)-appstream-source-rpms
dnf install git
git config --global user.name " Full Name " git config --global user.email " email@example.com "
git config --global core.editor command
man git man gittutorial man gittutorial-2
dnf group install "Development Tools"
dnf install llvm-toolset
dnf install gcc-gfortran
dnf install gdb valgrind systemtap ltrace strace
dnf install dnf-utils
stap-prep
dnf install perf papi pcp-zeroconf valgrind strace sysstat systemtap
stap-prep
systemctl enable pmcd && systemctl start pmcd
man gcc
gcc -c source.c another_source.c
gcc ... -g
man gcc
man gcc
gcc ... -O2 -g -Wall -Wl,-z,now,-z,relro -fstack-protector-strong -fstack-clash-protection -D_FORTIFY_SOURCE=2
gcc ... -Walloc-zero -Walloca-larger-than -Wextra -Wformat-security -Wvla-larger-than
gcc ... objfile.o another_object.o ... -o executable-file
mkdir hello-c cd hello-c
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
gcc hello.c -o helloworld
./helloworld Hello, World!
mkdir hello-c cd hello-c
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
gcc -c hello.c
gcc hello.o -o helloworld
./helloworld Hello, World!
mkdir hello-cpp cd hello-cpp
#include <iostream> int main() { std::cout << "Hello, World!\n"; return 0; }
g++ hello.cpp -o helloworld
./helloworld Hello, World!
mkdir hello-cpp cd hello-cpp
#include <iostream> int main() { std::cout << "Hello, World!\n"; return 0; }
g++ -c hello.cpp
g++ hello.o -o helloworld
./helloworld Hello, World!
gcc ... -l foo
gcc ... -I include_path
gcc ... -L library_path -l foo
gcc ... -I header_path -c
gcc ... -L library_path -l foo
./program
gcc ... -L library_path -l foo
gcc ... -L library_path -l foo -Wl,-rpath= library_path
./program
export LD_LIBRARY_PATH= library_path :USDLD_LIBRARY_PATH ./program
man ld.so
cat /etc/ld.so.conf
ldconfig -v
gcc ... path/to/libfoo.a
gcc ... -Wl,-Bstatic -l first -Wl,-Bdynamic -l second
gcc ... -l foo
objdump -p somelibrary | grep SONAME
gcc ... -c -fPIC some_file.c
gcc -shared -o libfoo.so.x.y -Wl,-soname, libfoo.so.x some_file.o
cp libfoo.so.x.y /usr/lib64
ln -s libfoo.so.x.y libfoo.so.x ln -s libfoo.so.x libfoo.so
gcc -c source_file.c
ar rcs lib foo .a source_file.o
nm libfoo.a
gcc ... -l foo
man ar
all: hello hello: hello.o gcc hello.o -o hello hello.o: hello.c gcc -c hello.c -o hello.o
CC=gcc CFLAGS=-c -Wall SOURCE=hello.c OBJ=USD(SOURCE:.c=.o) EXE=hello all: USD(SOURCE) USD(EXE) USD(EXE): USD(OBJ) USD(CC) USD(OBJ) -o USD@ %.o: %.c USD(CC) USD(CFLAGS) USD< -o USD@ clean: rm -rf USD(OBJ) USD(EXE)
mkdir hellomake cd hellomake
#include <stdio.h> int main(int argc, char *argv[]) { printf("Hello, World!\n"); return 0; }
CC=gcc CFLAGS=-c -Wall SOURCE=hello.c OBJ=USD(SOURCE:.c=.o) EXE=hello all: USD(SOURCE) USD(EXE) USD(EXE): USD(OBJ) USD(CC) USD(OBJ) -o USD@ %.o: %.c USD(CC) USD(CFLAGS) USD< -o USD@ clean: rm -rf USD(OBJ) USD(EXE)
make gcc -c -Wall hello.c -o hello.o gcc hello.o -o hello
./hello Hello, World!
make clean rm -rf hello.o hello
man make info make
gcc ... -g
man gcc
gdb -q /bin/ls Reading symbols from /bin/ls...Reading symbols from .gnu_debugdata for /usr/bin/ls...(no debugging symbols found)...done. (no debugging symbols found)...done. Missing separate debuginfos, use: dnf debuginfo-install coreutils-8.30-6.el8.x86_64 (gdb)
(gdb) q
dnf debuginfo-install coreutils-8.30-6.el8.x86_64
which less /usr/bin/less
locate libz | grep so /usr/lib64/libz.so.1 /usr/lib64/libz.so.1.2.11
dnf install mlocate updatedb
rpm -qf /usr/lib64/libz.so.1.2.7 zlib-1.2.11-10.el8.x86_64
debuginfo-install zlib-1.2.11-10.el8.x86_64
gdb program
ps -C program -o pid h pid
gdb -p pid
(gdb) shell ps -C program -o pid h pid
(gdb) attach pid
(gdb) file path/to/program
(gdb) help info
(gdb) br file:line
(gdb) br line
(gdb) br function_name
(gdb) br file:line if condition
(gdb) info br
(gdb) delete number
(gdb) clear file:line
(gdb) watch expression
(gdb) rwatch expression
(gdb) awatch expression
(gdb) info br
(gdb) delete num
strace -fvttTyy -s 256 -e trace= call program
ps -C program (...) strace -fvttTyy -s 256 -e trace= call -p pid
strace ... |& tee your_log_file.log
man strace
ltrace -f -l library -e function program
ps -C program (...) ltrace -f -l library -e function -p pid program
ltrace ... |& tee your_log_file.log
man ltrace
ps -aux
stap /usr/share/systemtap/examples/process/strace.stp -x pid
(gdb) catch syscall syscall-name
(gdb) r
(gdb) c
(gdb) catch signal signal-type
(gdb) r
(gdb) c
ulimit -a
DumpCore=yes DefaultLimitCORE=infinity
systemctl daemon-reexec
ulimit -c unlimited
dnf install sos
sosreport
coredumpctl list executable-name coredumpctl dump executable-name > /path/to/file-for-export
eu-unstrip -n --core= ./core.9814 0x400000+0x207000 2818b2009547f780a5639c904cded443e564973e@0x400284 /usr/bin/sleep /usr/lib/debug/bin/sleep.debug [exe] 0x7fff26fff000+0x1000 1e2a683b7d877576970e4275d41a6aaec280795e@0x7fff26fff340 . - linux-vdso.so.1 0x35e7e00000+0x3b6000 374add1ead31ccb449779bc7ee7877de3377e5ad@0x35e7e00280 /usr/lib64/libc-2.14.90.so /usr/lib/debug/lib64/libc-2.14.90.so.debug libc.so.6 0x35e7a00000+0x224000 3ed9e61c2b7e707ce244816335776afa2ad0307d@0x35e7a001d8 /usr/lib64/ld-2.14.90.so /usr/lib/debug/lib64/ld-2.14.90.so.debug ld-linux-x86-64.so.2
eu-readelf -n executable_file
gdb -e executable_file -c core_file
(gdb) symbol-file program.debug
sysctl kernel.core_pattern
kernel.core_pattern = |/usr/lib/systemd/systemd-coredump
pgrep -a executable-name-fragment
PID command-line
pgrep -a bc 5459 bc
kill -ABRT PID
coredumpctl list PID
coredumpctl list 5459 TIME PID UID GID SIG COREFILE EXE Thu 2019-11-07 15:14:46 CET 5459 1000 1000 6 present /usr/bin/bc
coredumpctl info PID
coredumpctl debug PID
Missing separate debuginfos, use: dnf debuginfo-install bc-1.07.1-5.el8.x86_64
coredumpctl dump PID > /path/to/file_for_export
ps -C some-program
gcore -o filename pid
sosreport
(gdb) set use-coredump-filter off
(gdb) set dump-excluded-mappings on
(gdb) gcore core-file
dnf install gcc-toolset- N
dnf list available gcc-toolset- N -\*
dnf install package_name
dnf install gcc-toolset-13-annobin-annocheck gcc-toolset-13-binutils-devel
dnf remove gcc-toolset- N \*
scl enable gcc-toolset- N tool
scl enable gcc-toolset- N bash
scl enable gcc-toolset-12 'gcc -lsomelib objfile.o'
scl enable gcc-toolset-12 'gcc objfile.o -lsomelib'
scl enable gcc-toolset-12 'ld -lsomelib objfile.o'
scl enable gcc-toolset-12 'ld objfile.o -lsomelib'
cc1: fatal error: inaccessible plugin file opt/rh/gcc-toolset-12/root/usr/lib/gcc/ architecture -linux-gnu/12/plugin/gcc-annobin.so expanded from short plugin name gcc-annobin: No such file or directory
cd /opt/rh/gcc-toolset-12/root/usr/lib/gcc/ architecture -linux-gnu/12/plugin ln -s annobin.so gcc-annobin.so
scl enable gcc-toolset-13 'gcc -lsomelib objfile.o'
scl enable gcc-toolset-13 'gcc objfile.o -lsomelib'
scl enable gcc-toolset-13 'ld -lsomelib objfile.o'
scl enable gcc-toolset-13 'ld objfile.o -lsomelib'
cc1: fatal error: inaccessible plugin file opt/rh/gcc-toolset-13/root/usr/lib/gcc/ architecture -linux-gnu/13/plugin/gcc-annobin.so expanded from short plugin name gcc-annobin: No such file or directory
cd /opt/rh/gcc-toolset-13/root/usr/lib/gcc/ architecture -linux-gnu/13/plugin ln -s annobin.so gcc-annobin.so
scl enable gcc-toolset-14 'gcc -lsomelib objfile.o'
scl enable gcc-toolset-14 'gcc objfile.o -lsomelib'
scl enable gcc-toolset-14 'ld -lsomelib objfile.o'
scl enable gcc-toolset-14 'ld objfile.o -lsomelib'
cc1: fatal error: inaccessible plugin file opt/rh/gcc-toolset-14/root/usr/lib/gcc/ architecture -linux-gnu/14/plugin/gcc-annobin.so expanded from short plugin name gcc-annobin: No such file or directory
cd /opt/rh/gcc-toolset-14/root/usr/lib/gcc/ architecture -linux-gnu/14/plugin ln -s annobin.so gcc-annobin.so
podman login registry.redhat.io Username: username Password: ********
podman pull registry.redhat.io/rhel8/gcc-toolset- <toolset_version> -toolchain
podman images
podman run -it image_name /bin/bash
podman login registry.redhat.io Username: username Password: ********
podman pull registry.redhat.io/rhel9/gcc-toolset-14-toolchain
podman run -it registry.redhat.io/rhel9/gcc-toolset-14-toolchain /bin/bash
bash-4.4USD gcc -v gcc version 14.2.1 20240801 (Red Hat 14.2.1-1) (GCC)
bash-4.4USD rpm -qa
gcc -fplugin=annobin
gcc -iplugindir= /path/to/directory/containing/annobin/
gcc --print-file-name=plugin
clang -fplugin= /path/to/directory/containing/annobin/
gcc -fplugin=annobin -fplugin-arg-annobin- option file-name
gcc -fplugin=annobin -fplugin-arg-annobin-verbose file-name
clang -fplugin= /path/to/directory/containing/annobin/ -Xclang -plugin-arg-annobin -Xclang option file-name
clang -fplugin=/usr/lib64/clang/10/lib/annobin.so -Xclang -plugin-arg-annobin -Xclang verbose file-name
annocheck file-name
annocheck directory-name
annocheck rpm-package-name
annocheck rpm-package-name --debug-rpm debuginfo-rpm
annocheck --enable-built-by
annocheck --enable-notes
annocheck --section-size= name
annocheck --enable-notes --disable-hardened file-name
objcopy --merge-notes file-name
cc1: fatal error: inaccessible plugin file opt/rh/gcc-toolset-12/root/usr/lib/gcc/ architecture -linux-gnu/12/plugin/gcc-annobin.so expanded from short plugin name gcc-annobin: No such file or directory
cd /opt/rh/gcc-toolset-12/root/usr/lib/gcc/ architecture -linux-gnu/12/plugin ln -s annobin.so gcc-annobin.so
#include <signal.h> #include <stdio.h> static const char *strsig (int sig) { return sys_siglist[sig]; } int main (int argc, char *argv[]) { printf ("%s\n", strsig (SIGINT)); return 0; }
#include <signal.h> #include <stdio.h> #include <string.h> static const char *strsig (int sig) { return strsignal(sig); } int main (int argc, char *argv[]) { printf ("%s\n", strsig (SIGINT)); return 0; }
#define _GNU_SOURCE #include <signal.h> #include <stdio.h> #include <string.h> static const char *strsig (int sig) { const char *r = sigdescr_np (sig); return r == NULL ? "Unknown signal" : r; } int main (int argc, char *argv[]) { printf ("%s\n", strsig (SIGINT)); printf ("%s\n", strsig (-1)); return 0; }
#include <stdio.h> #include <errno.h> static const char *strerr (int err) { if (err < 0 || err > sys_nerr) return "Unknown"; return sys_errlist[err]; } int main (int argc, char *argv[]) { printf ("%s\n", strerr (-1)); printf ("%s\n", strerr (EINVAL)); return 0; }
#include <stdio.h> #include <errno.h> static const char *strerr (int err) { return strerror (err); } int main (int argc, char *argv[]) { printf ("%s\n", strerr (-1)); printf ("%s\n", strerr (EINVAL)); return 0; }
#define _GNU_SOURCE #include <stdio.h> #include <errno.h> #include <string.h> static const char *strerr (int err) { const char *r = strerrordesc_np (err); return r == NULL ? "Unknown error" : r; } int main (int argc, char *argv[]) { printf ("%s\n", strerr (-1)); printf ("%s\n", strerr (EINVAL)); return 0; }