How do I turn off the gcc preprocessor on linux? - c++

I have googled turning off the gcc preprocessor on linux for a good while now (using that exact phrase) and everything has been irrelevant. For example I want to turn off everything except the preprocessor (the opposite of what I want) or pressurising warnings. Does anyone know of a way to disable the preprocessor? I found one that Facebook developed and claimed is faster, and I would like to test it out.

Name your file program.i instead of program.c and it will be treated as already pre-processed by GCC/Clang and sent directly to the compiler.
Example:
$ cat t.i
int printf(const char *f, ...);
int main(){
printf("hello world\n");
}
$ gcc t.i && ./a.out
hello world

I tested what lornix said in a comment, and it works:
Name the other/newer preprocessor "cpp" and put it in your path, and rename the original cpp to cpp-other or cpp-orig. It'll work great considering you are attempting to replace cpp anyways.

Related

Having difficulties compiling simple c++ program on the command line with multiple files; maybe a linker error?

Sorry for the simple question. I am attempting to learn more c++ at a fundamental level. I have always used VS in the past, and I am trying to learn the command line and compile, navigate, etc. with it.
I started with "hello world" and was able to compile it with gcc/clang, then run it with the expected results.
I then slightly reworked this and made a new header/cpp file to do the output part of hello world, and then call that from the main function, described below:
main.cpp:
#include "MyClass.h"
int main(){
foo();
return 0;
}
MyClass.h
#pragma once
void foo();
MyClass.cpp
#include "MyClass.h"
#include <iostream>
void foo(){
std::cout << "Hello World\n";
}
I then have tried to compile with gcc and clang as follows:
clang -Wall -g main.cpp MyClass.cpp
I have tried the same with GCC, and have also tried various invocations of this, such as using -c:
clang -Wall -g -c main.cpp
clang -Wall -g -c MyClass.cpp
Each and every time, I get an error
λ clang -Wall -g MyClass.cpp main.cpp
main.cpp:13:1: error: use of undeclared identifier 'foo'
foo();
^
1 error generated.
I get this same error whether using gcc or clang.
I also tried from scratch on my laptop, to see if there was some more global issue, but I still get the same problem.
I have also tried on the basic Windows command line as well.
Other areas on StackOverflow demonstrate simple ways of compiling multiple files from the command line, and I have tried as they show, but still get errors.
I also know that "make" is something I need to learn as well, however, I just want to make sure I understand what my make file is doing before I dive into that.
I feel like it must be something trivial that I just cannot figure out.
Thank you to Andreas for the suggestion of looking at the preprocessor output. And thank you to everyone for the suggestions.
The pre-processor output did not make sense to what I was compiling.
I was using VSCode, in this case, as a text editor, making brand new files in my folder after launching it from the command line. I thought the files I created in VSCode directly into the folder (named main.cpp, for example), would produce a regular text file. However, for some reason, it did not.
Essentially, I recreated the above program in notepad and was easily able to compile it using the commands I used above. I guess VSCode may not be perfect for me as a pure text editor or I should figure out if there are settings to change to accomplish my goal.
Thank you all again for your time and consideration.
Use extern on your function. Also make sure you're compiling with c++ and not c; i.e. g++.
MyClass.h
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
extern void foo();
#ifdef __cplusplus
}
#endif

MinGW doesn't like comments

I have a compiling Problem using "code::blocks" on Windows 7. My C-code is:
//whatever
int main(void){return 0;}
//this is blank line
The MinGW commandline is:
gcc.exe -Wall -g -ansi -c C:...\Test\main.c -o obj\Debug\main.o
If i try to compile this, I get the error:
C:\...\Test\main.c|1|error: expected identifier or '(' before '/' token
(I wanted to post a picture here, but not enough reputation ...)
There are only 3 lines of code in my source file. (The last just contains \0, but I didn't know, how to add a blank line). I use code::blocks as IDE. I used notepad++ to search for non-printable chars, but with no meaningful results. I use the MinGW compiler that is available as bundled download with code::blocks. I corrected the "toolchain executables" and the compiler worked fine until now.
It's not the first time I had this problem. I remember having it on another computer before and solved it by retyping the whole source file (which I don't want to do every time).
The way the error was provoked is just commenting and uncommenting code for a while (I tried out some things and commented previous tests away). And than, out of the blue, this error appeared.
Sorry, if my grammar is bad. English is not my mother tongue.
I appreciate any given help!
Thanks in advance,
Nils
Remove the compiler option -ansi.
ANSI C does not understand //.
From the gcc documentation:
3.4 Options Controlling C Dialect
[...]
-ansi
In C mode, this is equivalent to -std=c90. In C++ mode, it is equivalent to -std=c++98.
[...] For the C compiler, it disables recognition of C++ style ‘//’ comments [...]
In code blocks do this
Make sure that highlighted option is unchecked.(This is same as above ans. removing -ansi. option.)
Settings->compiler
For compiling c use
/* C-style comments!
*/

error occured when fortran code called c code

I have used a method in a reference book about fortran codes calling c codes. The method is that if your function name in the c codes are capitalized, you may not make additional changes in your fortran code. The following is my code.
Source1.f90:
program main
implicit none
call c_subprint()
endprogram
ccode.c:
#include <stdio.h>
#include <string.h>
#ifdef _cplusplus
extern "C" {
#endif
void _stdcall C_SUBPRINT()
{
int a;
printf("%s\n","kk");
scanf("%d",&a);
}
#ifdef _cplusplus
}
#endif
The error message is that the main function cannot find out _C_SUBPRINT(LNK2019). But I have already added the .lib generated by the c code to the fortran code. And my code is almost the same as that in the reference book. what is wrong?
Different Fortran compilers generate calls differently. Many add underscores to routine names to avoid name conflicts with C-code. Its very hard to say whether or not you should exactly follow the example of an unnamed reference book. In the past, to mix Fortran and C, you have to understand some of the compiler internals. Here the Fortran compiler appears to have added an underscore to the start of the name. You could probably fix the problem by adding that in your C++ code. But that won't be portable and in principle could change with compiler versions. There is a better way: the Fortran ISO_C_Binding. This is part of the Fortran language standard and therefore standard and portable. Examples are given in the gfortran manual in the Chapter "Mixed-Language Programming" and in other questions on Stackoverflow.
From the inclusion of _stdcall I deduce you are probably using a Windows platform. Unfortunately I'm working on a Mac, so things may not be exactly the same. However, the following works for me (small changes from your code) to get a mix of Fortran and C code working together. Note - on my Mac, the gcc compiler does not include Fortran, so I used a separate Fortran compiler gfortran which I downloaded from a link at http://hpc.sourceforge.net - where instructions for installation could also be found.
program src1.f90:
program main
implicit none
call C_SUBPRINT()
endprogram
program ccode.c:
#include <stdio.h>
#include <string.h>
void c_subprint_()
{
printf("hello world!\n");
}
Note I took out the #ifdef sections - I was using pure C, so it was not needed; more importantly, note the underscore after the function name
I compiled these two modules as follows:
gcc -c ccode.c -o ccode.o
gfortran src1.f90 ccode.o -o hello
After which I can run
./hello
And get the expected output:
hello world!
Without the underscore, the linker complains about unknown symbols. There is an option in the compiler to turn off "name mangling" with the underscore. You can check your specific compiler to see how that would be done - but that's almost certainly the problem you are having.

How configure include path and use standard library with gcc compiler?

I know this topic was few times there, but I can't get satisfactory answer.
C:\Users\Krzysiek>gcc test.c
test.c:3:20: fatal error: iostream: No such file or directory
compilation terminated.
This is what I try to do
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
Simple program with "include"
I've heard of LIBRARY_PATH. So I've setted that. Still this same error I have.
GCC provides wrappers around calling its various compilers.
You are using gcc, which is for C (and consequently will not include or link the C++ standard library; the compiler would go on to complain about the rest of your code, too, since it's not valid C);
Use g++, which is for C++.
Also try to use a conventional extension for C++ source files, which is .cc, .cxx or .cpp.
Use g++ instead: it will link to the c++ standard library.
When you use the gcc command, gcc looks at the file extension to decide which language to use to compile. As you used a .c file, gcc will switch by default to C.
# Use the C compiler
gcc test.c
# Use the C++ compiler
gcc test.cpp
To choose a different language, you can use the -x option:
# Use the C++ compiler even if the extension is .c
gcc -xc++ test.c
Another method of using the C++ compiler is to use g++ in the command line. This is the preferred way, as it links with the correct libraries.
# Use the C++ compiler
g++ test.c

Compiling a C++ program with GCC

How can I compile a C++ program with the GCC compiler?
File info.c
#include<iostream>
using std::cout;
using std::endl;
int main()
{
#ifdef __cplusplus
cout << "C++ compiler in use and version is " << __cplusplus << endl;
#endif
cout <<"Version is " << __STDC_VERSION__ << endl;
cout << "Hi" << __FILE__ << __LINE__ << endl;
}
And when I try to compile info.c:
gcc info.C
Undefined first referenced
symbol in file
cout /var/tmp/ccPxLN2a.o
endl(ostream &) /var/tmp/ccPxLN2a.o
ostream::operator<<(ostream &(*)(ostream &))/var/tmp/ccPxLN2a.o
ostream::operator<<(int) /var/tmp/ccPxLN2a.o
ostream::operator<<(long) /var/tmp/ccPxLN2a.o
ostream::operator<<(char const *) /var/tmp/ccPxLN2a.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
Isn't the GCC compiler capable of compiling C++ programs?
On a related note, what is the difference between gcc and g++?
gcc can actually compile C++ code just fine. The errors you received are linker errors, not compiler errors.
Odds are that if you change the compilation line to be this:
gcc info.C -lstdc++
which makes it link to the standard C++ library, then it will work just fine.
However, you should just make your life easier and use g++.
Rup says it best in his comment to another answer:
[...] gcc will
select the correct back-end compiler
based on file extension (i.e. will
compile a .c as C and a .cc as C++)
and links binaries against just the
standard C and GCC helper libraries by
default regardless of input languages;
g++ will also select the correct
back-end based on extension except
that I think it compiles all C source
as C++ instead (i.e. it compiles both
.c and .cc as C++) and it includes
libstdc++ in its link step regardless
of input languages.
If you give the code a .c extension the compiler thinks it is C code, not C++. And the C++ compiler driver is called g++, if you use the gcc driver you will have linker problems, as the standard C++ libraries will not be linked by default. So you want:
g++ myprog.cpp
And do not even consider using an uppercase .C extension, unless you never want to port your code, and are prepared to be hated by those you work with.
You should use g++ instead of gcc.
By default, gcc selects the language based on the file extension, but you can force gcc to select a different language backend with the -x option thus:
gcc -x c++
More options are detailed on the gcc man page under "Options controlling the kind of output". See e.g. gcc(1) - Linux man page (search on the page for the text -x language).
This facility is very useful in cases where gcc can't guess the language using a file extension, for example if you're generating code and feeding it to gcc via standard input.
The difference between gcc and g++ are:
gcc | g++
compiles C source | compiles C++ source
Use g++ instead of gcc to compile you C++ source.
If I recall correctly, gcc determines the filetype from the suffix. So, make it foo.cc and it should work.
And, to answer your other question, that is the difference between "gcc" and "g++". gcc is a frontend that chooses the correct compiler.
An update with my GCC version 10.3.0 on Ubuntu. Even if I add -lstdc++ or use the correct extension, cc1plus must be installed on the system. Otherwise this error shows up:
gcc: fatal error: cannot execute ‘cc1plus’: execvp: No such file or directory
One way to get this is to install g++ even if you're not going to use it directly, e.g. sudo apt install g++.
Now, if I use the extension .cc, I can just call gcc directly, however, warnings and errors still show up. To use gcc cleanly, with a .c extension I have to call it like this:
gcc -x c++ info.c -lstdc++
Shorter if I use the .cc extension, like the accepted answer:
gcc info.cc -lstdc++
Or like others have said, just use g++ info.c instead. No extra parameters are needed to indicate C++, and it works with .c, .cc and .cpp extensions.
It worked well for me. Just one line code on the Windows command line (CMD).
First, confirm that you have installed gcc (for C) or g++ (for C++) compiler.
On the command line, for gcc, type:
gcc --version
On the command line, for g++, type:
g++ --version
If it is installed then proceed.
Now, compile your .c or .cpp using the command line.
For C syntax:
gcc -o exe_filename yourfilename.c
Example:
gcc -o myfile myfile.c
Here exe_filename (myfile in example) is the name of your .exe file which you want to produce after compilation (Note: I have not put any extension here). And yourfilename.c (myfile.c in example) is the your source file which has the .c extension.
Now go to the folder containing your .c file. Here you will find a file with the .exe extension. Just open it. Hurray...
For C++ syntax:
g++ -o exe_filename yourfilename.cpp
After it, the process is the same as for the C syntax.
For a .cpp File:
g++ myprog.cpp -o myprog