CPP codes linking in VScode with Mingw-w64 in Windows 10 - c++

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.

Related

how do i set up a project folder in vscode and link all cpp files [duplicate]

This question already has answers here:
How do I set up Visual Studio Code to compile C++ code?
(14 answers)
Closed last month.
i'm new to cpp and just moved to classes, but now im stuck here wondering how do i link 2 or more cpp files to make a project(main.cpp - for main, abc.h - for defining class , abc.cpp- for defining methods outside the class). For now i use the command g++ main.cpp abc.cpp -o main to link all the files but as my project grows larger it will become more tedious to enter all the file names.Some guy here told if i create a project i should be able to compile as usual without compiling manually.So how do i do that in vscode?
You should look for the concept of makefile, and especially cmake which will help you for your compilation problem.
For bigger projects you can use makefile for compiling easily and faster. So in vs code I recommend you to use this extension. VS Code C/C++ Makefile Project
Firstly,
C/C++ Make: INIT Project
After,
C++ Project
In makefile you can customize your settings.
Also you can watch this tutorial video to understand easily this process in vs code Debugging Makefile Project with Visual Studio Code

Manually compile QT project with g++

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!

How do I access save-temps option in Visual Studio?

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.

Windows build with -mwindows flag no executable

I had problems building a program I'm working on in C++ because MinGW files and Windows SDK file were conflicting and I found the solution by adding the -mwindows flag for the linker to the build options. This works: now I can build the project without errors.
But now the compiler doesn't output any executable file (.exe).
My question is: why? And what exactly the -mwindows does?
Edit: The output of the build phase is reported in a text file in this Dropbox folder (I couldn't copy it here because it's more than 2800 lines): https://www.dropbox.com/sh/1ud85a3fktb62v6/AAB3L8Vp5VcKPJqCPyOXtzdca?dl=0
I already tried to create a new 'Hello World' project and build it: the executable file is created and I can run it, so I think the cause of my problem is that flag and not a particular setting on my IDE (CLion).

How to output preprocessed code AND compile it (Visual Studio)

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