How to get preprocessed C++ code from DevC++ under Windows XP? I've read about creating gcc -E file.cpp file, but I still can't connect the dots, how to run this file? After I've compiled it everything went as usual.
You simply can't 'run' a preprocessed file. You can just compile and run it, or inspect it for what the preprocessor produced.
E.g. when using GCC you can run
gcc file.cpp -E <all preprocessor options as set from the IDE> -o file_preprocessed.cpp
to get the file_preprocessed.cpp file for inspection.
I don't know for the dev-c++ IDE in particular, but usually you'll get a representation of the actually used compiler flags of a project in the project settings somewhere.
run
gcc file.cpp -E -o file_preprocessed.cpp
then you can open file_preprocessed.cpp to see the output
Related
I'm learning C++ with VScode with Mingw-w64 in Windows 10.
Today, I watched how to make header files and build the separated codes in Visual Studio 2019.
I wanted to implement this process in Visual Studio Code editor, but I met linking error.
My naive solution is like
~$ g++ -c .\main.cpp .\add.cpp
~$ g++ .\main.o .\add.o -o test.exe
First, compiling the source codes into object files.
Second, linking those object files to an execution file.
It works anyway, but when I tried to run the debugging mode, it doesn't work.
I think I need to edit launch.json file, but I have no idea...
The following images are about my situation.
How can I build those separated codes at once?
In this time, I just described the path of the definition code into 'tasks.json' file.
After then, built the main code again, so that it worked.
I am currently working on a polymorphic engine that works for c++ projects. What i am doing is that i am
(1) generating assembly files from .cpp files
(2) generating object files from assembly files
(3) loading object files into an .exe
All this is done using the g++ compiler through the command line. Now, all this works fine for a regular win32 c++ console application project created in visual studio, but i also need to adapt my polymorph engine to be able to build QT projects (GUI) with the same method of approach listed above.
I am unable to find commands to replicate this for QT projects. Below is the commands i am currently using in order to manually compile a console application project built in Visual studio.
(1) g++ -S -masm=intel -o my_asm_output.s main.cpp
// generate assembly from main.cpp file
(2) g++ -c -o my_obj_output.o assembly.s
// generate object file from assembly.s
(3) g++ -o my_exe_output.exe my_obj_file.o
// generate executable by loading object file into it
Thanks!
I am a complete programming (and generally IT) noob and I am learning C++ using Visual Studio environment. I would like to explore the compiler, and see my temporary files (ending with .ii, .s and .o).
My textbooks tell me to instruct the compiler to save using the -save-temps option. What I don't understand is how do I actually access this -save-temps option? Is there some toggle option I click in Visual Studio to do this? Where do I find it? Please bear in mind I am completely new to the Visual Studio environment.
You should use g++ (GNU C++ Compiler) to compile your source code and not Visual Studio.
To get .o files run g++ main.c -o main.o. This will give you the object file.
It is incomprehensible as it is a binary file not a readable text file.
To get .s files run g++ -S main.c -o main.s. This will give you the assembly file.
This file is readable as it is the assembly of your source code.
when I work on my IDE (visual studio or xCode) I never include cpp files (only h files) and everything compiles perfectly.
however, when I compile on the cmd/terminal (mostly on my raspberry pi) if I don't include the cpp files the compiler throws many errors because he didn't read the cpp files.
why is that? how does the ide knows to include the currect cpp files? and is there a way to find out if you are compiling through an ide or terminal?
something like:
#ifdef IDE_KEYWORD
#endif
The IDE will typically compile each source file (what you are describing as a "cpp file") separately, to produce an object file for each, and then link all the object files (and other libraries). Mechanically, the IDE does that by invoking the compiler separately for each source file listed in the "project file" and then linking.
When compiling from the command line, it is necessary to specify EVERY source file on the command line - the IDE isn't helping with that, so you need to do it manually.
One option is to compile every source file separately (specify each on a separate command line) and then another command to link all the object files together to produce the executable. In effect, this (more or less) imitates what an IDE does.
A second option is to list EVERY source file on a single command line - which will cause the compiler to compile each one sequentially and then (depending on what command line options you specify) link the object files together.
IDEs track what files are in the project/workspace and what ones are not. For instance, Visual Studio has a block in the vcxproj file which stores all the compilation units as such
<ClCompile Include="yourFile.cpp" />
having an ifdef to determine if you are using an IDE should be just a matter of adding IDE_KEYWORD to your preprocessor definitions inside your IDE. then if you compile outside of that IDE, the IDE_KEYWORD wont be defined.
If you are in Visual Studio you can
#ifdef _MSC_VER
// your code
#endif
If not I suppose there is no way.
The error may occur because you are not compiling all the .cpp files and/or are linking not all object files.
If using g++ g++ -c file.cpp -o file.o for every file to compile them. Then g++ file1.o file2.o fileN.o -o finalExecutable.exe for linking.
If this seems complicated look into Makefiles. They are awesome!
I'm generating preprocessor output (.i) from Visual Studio, but also want to do the actual build. Is there a combination of flags that will both output the .i file without then stopping the compiler from going ahead with the build as normal?
This is currently just C++ but will probably want to use this with CUDA later, so prefer answers that work within Visual Studio rather than require command line (unless it works for CUDA too).
The point of this is to save the time it takes to do Project->Properties->Config Props->C/C++->Preprocessor->Generate Preprocessed File Yes/No plus Rebuild. Particularly it is irksome to me that the preprocessor has to run twice, so a solution that somehow generates the .i file in part 1 and then compiles that file in part 2 would be fine.
The exact version of Visual Studio I'm using is VS 2008 Express
You can add a custom build step or a new target to dump the preprocess after the code is built by definition the dumped preprocessor output is what is built.
You can create a custom build config that does pre-processing and then define a batch build that builds the pre-processed version followed by the actual compiled / linked version.
Use the switch /P Example- cl /C sample.c
The above will generate a .I file with same name (sample.I). Now to compile the .I file, just rename .I file to .C then do cl /c to compile and generate an object file.
If your running GNU gcc/g++ compiler, then configure a Makefile target "recipe" with the following code
gcc -save-temps -dumpbase save-foo -c foo.c
Reference can be found in the terminal man pages for gcc.
man gcc
section:
-dumpbase dumpbase
This will create a separate .i file and compile "foo" to object code. That code can then be used as a prerequisite for your target "goal" executable. Link below explains the vocabulary I used.
https://www.gnu.org/software/make/manual/html_node/Simple-Makefile.html#Simple-Makefile