Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a Preprocessed C/C++ file machine-specific?

Say I am on a linux machine, and I run the following command:

cpp main.cpp > main_p.cpp

Where main.cpp is a c++/c file. Would I be able to take main_p.cpp and compile/execute it on any other machine? Or is this program compiler-specific?

Edit 1: cpp is the C preprocessor, as defined by the cpp(1) man page.

like image 366
SealsRock12 Avatar asked May 07 '26 09:05

SealsRock12


1 Answers

Preprocessor output is very much system specific, particularly if standard header files are involved.

The standard header files in particular have a lot of system specific things in there as they are considered part of the implementation and as such can have a lot of implementation specific things.

For example, the following program:

#include<stdio.h>

int main()
{
    printf("hello\n");
    return 0;
}

When passed through the preprocessor on CentOS 7 results in an 843 line file. The first few lines look like this:

# 1 "x1.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "/usr/include/stdc-predef.h" 1 3 4
# 1 "<command-line>" 2
# 1 "x1.c"
# 1 "/usr/include/stdio.h" 1 3 4
# 27 "/usr/include/stdio.h" 3 4
# 1 "/usr/include/features.h" 1 3 4
# 375 "/usr/include/features.h" 3 4
# 1 "/usr/include/sys/cdefs.h" 1 3 4
# 392 "/usr/include/sys/cdefs.h" 3 4
# 1 "/usr/include/bits/wordsize.h" 1 3 4
# 393 "/usr/include/sys/cdefs.h" 2 3 4
# 376 "/usr/include/features.h" 2 3 4
# 399 "/usr/include/features.h" 3 4
# 1 "/usr/include/gnu/stubs.h" 1 3 4
# 10 "/usr/include/gnu/stubs.h" 3 4
# 1 "/usr/include/gnu/stubs-64.h" 1 3 4
# 11 "/usr/include/gnu/stubs.h" 2 3 4
# 400 "/usr/include/features.h" 2 3 4
# 28 "/usr/include/stdio.h" 2 3 4

If I take this resulting file and attempt to compile it using the cl command in Visual Studio a long list of errors are generated.

Taking the original source file and running it through the Visual Studio preprocessor has output that starts like this:

#line 1 "x1.c"
#line 1 "C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.10240.0\\ucrt\\stdio.h"







#pragma once


#line 1 "C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.10240.0\\ucrt\\corecrt.h"







#pragma once

#line 1 "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\INCLUDE\\vcruntime.h"

As you can see the output is very different. Both compilers have different ways of denoting which source code line corresponds to a given line in the preprocessor output. And that's just one example of the differences.

like image 111
dbush Avatar answered May 09 '26 22:05

dbush