edit SideBar

Main / Gcc

Data Types

ISO C99 supports data types for integers that are at least 64 bits wide, and as an extension GCC supports them in C90 mode and in C++. Simply write long long int for a signed integer, or unsigned long long int for an unsigned integer. To make an integer constant of type long long int, add the suffix LL to the integer. To make an integer constant of type unsigned long long int, add the suffix ULL to the integer.

Command Line Considerations

GCC is very particular about where you place arguments and directives.

gcc <compiler_options> <source_file> <linker_options> <output_file>

This won't work:
gcc -D_FILE_OFFSET_BITS=64 -static -Imbedtls/include/mbedtls -Imbedtls/include/ -Lmbedtls/library -lmbedtls -lmbedx509 -lmbedcrypto main.cpp -o tg-crypto

But this will:
gcc -D_FILE_OFFSET_BITS=64 -static -Imbedtls/include/mbedtls -Imbedtls/include/ main.cpp -Lmbedtls/library -lmbedtls -lmbedx509 -lmbedcrypto -o tg-crypto


The -M flag will generate information for a .c file and the .o object it will create when compiled, such as local and system header file dependencies. -MM will ignore the system include dependencies and only show local dependencies. In both cases, these are used when not compiling.

If you'd like to actually do the compilation, then use -MD or -MMD instead and the resulting information is exported to the .d file. The .d file is a Make dependency file, in plain text.

-MF"filename" can be used with the above to specify where to write the dependency information (i.e. your own .d file).

-MT"filename" can be used with the above to specify the target object file name.


gcc -o test -MMD -MT"test.o" -MF"test.d" test.c

-fpic of -fPIC is for position-independent code, so jump destination addresses would be relative rather than absolute, and this makes the resulting object file suitable for inclusion in a library so that it can be loaded anywhere in case something else would be loaded at a non-PIC address already. Why not always use it? It requires an extra operation to find the destination address, so it's a bit slower to execute.

Difference between -pthread and -lpthread

-lpthread is older, and some claim is a solution for a problem that no longer exists. Where I've had to use it is with older 3.14 Linux embedded kernels running threaded applications of the C++11/14 era.

In the old days there were proprietary implementations of Pthreads API that weren't POSIX-compliant, like LinuxThreads. POSIX standard merely says that if one wants POSIX-compliant behaviour, then one must link with -lpthread.
Using the -lpthread option only causes the pthread library to be linked - the pre-defined macros don't get defined. Bottom line: you should use the -pthread option.
Later versions of compilers like gcc and clang (and, probably, all Linux-compatible compilers) require using -pthread command line option for both compiling and linking POSIX-compliant multi-threaded applications and that is what one must use.

-pthread adds support for multithreading with the pthreads library, setting flags for both the preprocessor and linker with one CL option.

Default Paths

For C: echo | gcc -xc -E -v -

For C++: echo | gcc -xc++ -E -v -

Page last modified on February 21, 2024, at 02:14 PM