Code blocks always runs main.cpp in a project - c++

I have code blocks 17and i created a new project.
Clicked create new project
2.Console application
3.C++
4.Gave it a name and stored it in an empty folder
5.Created new file inside the project and wrote some code.
When i build, it builds without errors. When i run it, It always runs main.cpp by default. How do i execute other files without having to run main.cpp everytime.

It doesn't run main.cpp - it runs the main() function, which might be in any named .cpp file - a file with the name main.cpp is not special as far as Code::Blocks, or C++ in general, is concerned. But a Code::Blocks project can only have one main() function.

From the looks of it, you could delete the main.cpp file from the project or just add the int main() function into the file that you want and remove the one in main.cpp

You need to build a file before you run it so insted of giving only run give build&run option.

Related

Whenever I try to update my code in C++(VSCode) with the g++ thing, it doesn't update what I put even after deleting the old .exe file

my code looks like ^^^ (comments because i make a file for notes on a new programming language im learning to look back at to help me)
I will put g++ filename.cpp and then ./a.exe after making a change and it doesn't change the output in the terminal. So for example if I put a :) at the end of the last string, it wouldn't update what it puts in the terminal even after doing g++ filename.cpp. I've tried deleting the older a.exe file before doing g++ but it doesn't fix the problem.
Please make sure you are actually using the GCC Compiler and that you are not getting errors at the moment of compile your code. You can use this to check the compiler: g++ --version; if you don't have the compiler installed, it will give you an error.
Also, at the moment of compile your code, you should use the g++ installed on your computer with the .cpp file that you want to compile and create the .exe file.
Example:
Here we have Basic00.cpp having a simple "hello world":
Then, we run g++ .\Basic00.cpp
It will create the a.exe file:
Now you just need to run .\a.exe:
And, if you want to add ":)" at the end of the string, you just need to add it, compile the code again (step 2) and run the exe file (step 4):
I recommend that you read this about how to use VS Code with G++ to compile and run your code without having issues. Very important: Remember that you need to compile your code every time that you make a change.
Edit with the new Image: I checked your image and the tab is showing a white circle, it means that you are not saving the changes, press Ctrl+S (to save) or configure Auto Save on File -> Auto Save, after saving compile your code again and run the .exe file.

My Visual Studio can't find files and so can't do basic operations. What's wrong with it?

Using C++, if I launch an 'empty project', create a new C++ file, and try to run it, I just get this error message:
Unable to run program 'C:\Users\User\source\repos\Project2\Debug\Project2.exe' The system cannot find the file specified
I go to the file it's referencing and the file IS there -- what??
Using a 'Console App' project is different: it will actually compile and run the code.
Similarly, I can't join new header files to the main file, not even in a 'Console App' project: if I write the code for doing #include "Header.h", I get a red line underneath #include, and if I hover over that it says:
cannot open source file "Header.h"
I'm new to coding, and don't know why I'm having such a seemingly absurd problem here. Help!
You should create a new project and then create a c++ file. While adding a file, make sure to select c++ console application and do not check empty project if you are new.
From the result you described, I suspect you used File -> New file to add that cpp file, right? This does NOT add that file to a project.
Instead, you should right-click the Project file in the Solution explorer, select Add new item (or Project -> Add new item menu), and choose the C++ file type.
This works!

FreeRTOS.h file not found - VS Code for Mac despite includePath modified & all filed in project folder

I am trying my best to include FreeRTOS into a new Project on VS Code for Mac.
I have created a directory for my project, added a main.cpp, configure the default build task for C/C++ clang. I have also created the c_cpp_properties.json.
My project consists of three folders; 'src' containing my main.cpp, and 'FreeRTOS' containing all of the source files for FreeRTOS (include & portable folders), and 'Demo' containing a demo app built for the CORTEX_ATSAM3X_Atmel_Studio board (that comes with the FreeRTOS download).
In my main.cpp file, I have an empty main function, that I can build successfully. The minute I try to add #include <FreeRTOS.h> to main.cpp, I am given the error FreeRTOS.h file not found.
What is interesting, is the first time I try to build, it recognises FreeRTOS.h, and the error I see is that it cannot find a file included in the FreeRTOS.h. To remedy this, I add the specific subdirectory to c_cpp_properties. I rebuild and then it shows me another file it cannot locate. So I work again manually amending another subdirectory. After 5 or 6 times, suddenly it fails to find FreeRTOS.h. When I delete all of the includePath entries except "${workspaceFolder}/**" I cannot get back to my starting point.
I have spent the last two days stuck on this, reading forums, starting my process again and I still cannot see where I am going wrong.
Probably I run into the same issue.
The problem was FreeRTOSConfig.h contains this definition
#define CMSIS_device_header "stm32h7xx.h"
which is next used in freertos_os2.h header (only):
#include CMSIS_device_header
and that is where build gets confused. It is not the perfect solution but removing the CMSIS_device_header variable and specifying stm32h7xx.h header directly in freertos_os2.h file solved my problem.

Problem with filestream path in C++ when copying files

Brace with me here. I'm trying to make a few simple, but useful C++ console apps that I want to run by starting them straight from cmd (like DiskPart for example). I have a finished product, compiled by gcc through VS Code powershell. The idea is: I have a main.exe and a data file stored in "veco/data.txt" next to the executable. The code works perfectly well when started from its folder where it was compiled, but when I try to copy it to WindowsApps folder (to be able to start it by typing its name in cmd) it just doesn't work...
Yes I did copy the folder too, I've tried to run it from powershell and I get 0 crash msgs...
Here's the part where I interact with the text file:
string s = _pathToExe + "veco\\data.txt";
ifstream is(s);
//i do stuff here
is.close();
where _pathToExe is path to the executable, without the actual name of the executable. ifstream is(s) seems to be the crashing point.
I compiled the code using two commands:
g++ -c <all .cpps>
g++ -o main.exe <all objs>
Can I make this work somehow? Any other suggestions are welcome too.
Note: I've tried copying to other locations, doesn't work there either.

Code::Blocks error multiple definition of 'main'

i tried to install my first program but it doesnt run. As the default program runs correctly. I created the new file as File>new>empty file and saved as .cpp but the program doesnt run. Ive matched the program from the book and the default one..
What is wrong
you can only have one main. By removing the Untitled.cpp file you will only have one file, main.cpp. you give your new file a name instead of taking the default name "Untitled.cpp". Or you can simply create an new project