Code::Block not creating a Debug Build? - c++

I have had this problem with Code::Blocks that when my project is still overly small (even 10 lines long), and I go to build "debug", the file will not be created. It doesn't tell me this in Code::Blocks. Basically, I go to hit play, and it will ask me to build my project even though I had clicked the "build" button about 2 seconds before...
It works fine with release build, however...
#include <iostream>
using namespace std;
int main()
{
cout << "Hello" << endl;
cout << "World!" << endl;
return 0;
}
Even a program as simple as that does work in release build, but not debug build (the file gets deleted).
Have I got something wrong with my settings? Note it was like this ever since I downloaded it, and I had no idea what was wrong (as it does debug build by default, so I didn't know I could use release build)
Anyone know?
Edit: yes, the file is part of a project file

Make sure you selected 'Console application' when creating the project instead of 'Empty Project'.

Also, when you create the project from new, make sure that the:-
'Create "Debug" configuration'
is enabled, and has valid paths showing etc.
On the previous dialog, where the new project tool asks for folder paths etc to put stuff, make sure you give it somewhere valid (that exists) etc, or make a new folder for it.
I've just built and run your bit of code as Debug and Release just fine, in C::B 16.01 on Windows. As a "Console" application. Using the defaults, other than I created a new folder for it all.
Regards.
DJB

Related

VSCode C++ Compiled exe was infected with Win32:TrojanX-gen[Trj]

I am setting up VSCode for C++, made some simple code (test.cpp) to test if things are working.
#include <iostream>
using namespace std;
int main(){
cout << "Hello";
}
When I click run code the console says "Access is denied" and my antivirus pops up and says the test.exe and tempCodeRunnerFile.exe was infected with Win32:TrojanX-gen[Trj].
What exactly is Win32:TrojanX-gen[Trj] and how do I grant access so my antivirus wont pop up?
I had this issue as well. Its simply because some antiviruses think that unsigned .exe files (which is the output of your CPP file after you compile) are some sort of malicious file. You do need to exclude this from your antivirus, and your best bet is to first find out which antivirus you have, and second go to the "exclusions" list and add either the folder you're working with, or the file itself. I'm assuming you have AVG lol Exclusions can be found in the settings of your antivirus. Right click on the icon and see if you can go to preferences or settings. Then look for exclusions, or even an advanced setting option, and keep looking for something along the exclusions nature.
Just add an exception to the folder which contains your c++ code. (The whole folder). It did the job for mine ^-^

Code Blocks "It seems like project has not been built yet. Do you want to build it now?"

Code blocks bee working fine for me. But yesterday whenever I try to compile a small program a window appears showing
It seems like project has not been built yet.
Do you wan to build it now?
I also found similar questions but in all of them not a single program is working. But in mine "hello world!" program and some other programs are working.
This is the code:
#include <iostream>
using namespace std;
void print(int b[], int s);
int main()
{
int a[5] = {1,2,3,4,5};
print(a, 5);
}
void print(int b[] , int s){
for(int i = 0; i < s; i++){
cout << b[i] << endl;
}
}
I installed code blocks in another computer and it's working fine.
I'm still learning C++.
What if I tell you that you should build your program to run it?
Wikipedia says:
In the field of computer software, the term software build refers
either to the process of converting source code files into standalone
software artifact(s) that can be run on a computer, or the result of
doing so. One of the most important steps of a software build is the
compilation process where source code files are converted into executable code.
It may be possible that your compiler is not linked properly to C::B or many other errors will be shown after you try to build your project, but for now (unless you post any build log) - you have to build your application in order to run it.
In CodeBlocks when a program is part of a project, it needs to built so that the compiler puts together all of the individual parts of the project.
If you do not want it, just make it a stand-alone program by opening it out of the project.
Pressing F9 would do just fine.
Solved , Anti-virus was deleting the .exe file for some reason. I put the project file folder in the "exclusion list" of anti-virus (or you can deactivate the anti-virus)
This happens if you add a New File to the project and try to build-run the code.
The solution is to create a new file outside the project and then right-click on the tab (where the name of the file writes, followed by .cpp) and select Add file to active project. In the pop-up GUI, check the Debug and Release options and hit OK. Now if you build and run, you will no longer have the message saying “It seems like project has not been built yet. Do you wan to build it now?”.

When debugging a visual c+ program, the file specified can't be executed

I made a simple hello world program. I clicked on "Start Debugging" and the window showed "Project is out of date. Would you like to build it?" As I click on "yes", the next window shows "There were build errors. Would you like to continue and run the last successful build?". I choose yes again, and it shows this window: (attached screenshot)enter image description here
There were build errors. Would you like to continue and run the last successful build?
The only correct answer to that question is "No". If you clicked "Debug", you obviously want to debug the current version of the source, not some stale old version that won't match what you're seeing in the editor.
Disable this nonsense message in Tools → Options → Projects and Solutions → Build and Run. For "On Run, when projects are out of date", set it to "Always build". For "On Run, when build or deployment errors occur", set it to "Do not launch".
I cannot think of a reason why you ever want the other options as default settings. If you want to launch an old, stale build, you can always do so manually.
I choose yes again, and it shows this window: "The system cannot find the file specified."
Yet another reason why this is a stupid setting. The second one in particular, the one that controls Run behavior when build errors occur.
What happens is, when you tried to build the project, the first step was to do a clean, which effectively means delete the old files. With the old files gone, it kicks off a build. The build fails, you get an error. You ask it to ignore the error and run an old version. But wait! The old version got deleted at the start of the build, so it no longer exists!
If a build fails, return to the IDE, fix the errors, and then relaunch to rebuild.
Bonus: The build error that you're getting is "fatal error C1010", which is a rather silly error that can be very confusing to those unaccustomed to Visual Studio. Basically, what it's telling you is that because you are using precompiled headers (the default for new projects), the very first line in every source file needs to be the inclusion of your precompiled header. By default, it is named stdafx.h, so the first line in your code file should be:
#include "stdafx.h"
This should go before you include the system header <iostream>. The precompiled header must be included at the very top of the file, or you'll get a build error.
If you do not like that, then you can turn off precompiled headers:
Right-click on your project in the Solution Explorer, and choose Properties.
At the top, click the "Configuration" combobox and select "All Configurations".
Expand "C/C++" in the tree view, and select "Precompiled Headers".
Set the top option, "Precompiled Header", to "Not Using Precompiled Headers".
Sorry: your last successful build was deleted earlier - possibly as a result of an attempted compile/link. You need to fix the source code that you've got now before you've got anything to debug...
It seems to appear that some .dll files are missing for the debug mode for many users.
You don't need to run the debug mode for this, if your program works on normal running, then let it run.
I also can see that you wrote void main() but in C++ the good syntax is int main() and terminated by a return 0; instruction. By the way, think about letting at least a space between #include and the libraries like <iostream> here.

New to Xcode can't open files in c++?

I've been using windows in a class I've been taking but I am trying to run a basic code to figure out how to open/close/input/output from files on Xcode and the code I usually use on visual studios isn't working any idea why? thanks!
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
string input;
fin.open("inputFile.txt");
if(fin.fail())
cout << "File failed to open." << endl;
fin >> input;
fout.open("outputFile.txt");
fout << input;
}
Put your .txt files in the same directory where your main.cpp file is (or anywhere you like).
In Xcode go to Product > Scheme > Edit Scheme > Run (on the left) > Options (middle top)
Down under Options for "Working Directory" check “Use custom working directory” and set it to the directory where you .txt files are located.
To work with the files, you will have to specify just file names, e.g. in_file.open("inputFile.txt"); no path is necessary.
Here's a completely different approach: Have Xcode copy the input file for you.
Select your project in Xcode
Select Build Phases
Click the '+' button to create a new Build Phase
Select New Copy Files Build Phase
Select Products Directory
Click the '+' button to add your file
Click Add Other
Select your input file and click Open
Check the Copy items… checkbox and click Finish
Now every time you build your project, the input file will be copied to the same folder as the executable no matter where it is built. Of course, to see the output file, you'll still need to find the executable in Finder.
The answers don't really explain the problem so I thought I'd do that.
When you pass a relative path like "inputFile.txt" to file APIs, it's treated as relative to the working directory when the program is executed. This is the same as the 'working directory' when you use cmd.exe or Terminal.app or command lines in general. The Unix command pwd ("print working directory") displays the current working directory. On Windows running the command cd with no arguments performs the same function. (On Unix running cd with no arguments will change the working directory to the user's home directory.)
When you run a program from the command line, the command line shell sets the program's working directory. When you run a program from within an IDE, the IDE sets the working directory. Since, unlike on a command line, there's no obvious answer for what the IDE should set as the working directory, Visual Studio and Xcode set the working directory to different locations by default: Visual Studio sets the working directory to $(ProjectDir), the directory containing the Visual Studio project file; Xcode sets the working directory to the build products directory, i.e. the location the executable was written to.
Some possible solutions to your problem are:
Do not use a relative path, and therefore don't depend on the working directory. This isn't much help in making the program more portable, because the absolute paths will also differ between platforms, and so you will still have to 'configure' the program for each platform. In fact using an absolute path is worse, because it means your source code must differ, whereas it would be better to keep that difference confined to each platform's build configuration.
Configure the IDE to use your desired working directory. Visual Studio can be configured by right clicking the project, selecting Configuration Properties > Debugging > Working Directory, and setting the working directory to the desired path (potentially using Visual Studio build variables).
nepete's answer describes how to configure the working directly set by Xcode.
Configure the IDE's build process to copy your data files to an appropriate location. In Visual Studio you would do this in a C++ project by configuring the project's Properties > Configuration Properties > Build Events.
SSteve's answer covers how to configure additional build steps in Xcode.
I'm guessing you have inputFile.txt in the folder that contains your source code. That's not going to work. You need to put it in the folder that contains the generated executable. To find that folder, right-click on your app under Products and select Show In Finder.
This image shows what it looks like for a command line program. It also shows the Finder window that was opened. As you can see, it is a different folder than the one containing the source code.
As suggested by nepete, edit the scheme, but use $PROJECT_DIR as the custom working directory. Helps with moving the project around, or working in two different environments (e.g., home and office).
BTW. $PROJECT_DIR is one of the Xcode Environment Variables, and also helps with passing file names as command line arguments to programs (settable under "Arguments" in the scheme).
I've struggled with the same problem today. I wanted to add C code to my Swift project and my file pointer was always NULL.
Unfortunately, in XCode 9 for iOS app, I couldn't change the working directory. Changing Build phases didn't help me either. After 4+ hours of trial and error, that's what I've come up with finally and it works:
when copying files to XCode, I've chosen "Create groups", but I needed to choose "Create folder references":
I created a new objective-c file (.m) and copied all my C code there.
I left untouched .h files (XCode generated bridging header and my own .h file with public functions declaration). Now my project structure looked like this:
In my dict.m file in place of previous plain c fopen part:
FILE *dic = fopen("dictionary.txt", "r");
I added obj-C code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"dictionary" ofType:#"txt"];
FILE *dic = fopen([filePath cStringUsingEncoding: NSUTF8StringEncoding], "r");
And it works now without any problem! It's just amazing!
ps I decided to write this answer in case it will help someone like me and will save them some time. If you know how to change working directory in XCode 9 for iOS, please, leave me a comment - now I am really curious why I can't find it.

"launching 'project' has encountered .." , project file does not exist

I wanted to write 'hello world' in eclipse c++, but it does not work
I go to Run configurations, what config options for c++ programs should i give?
I know I don't care about 'debug' - only 'release', but how to do that?
Here is what I did:
File->New->C++ Project
You will get a pop up window. Type the name of the project you want. Then, below it says executable and inside this folder, I have (by default I guess) Empty Project.
Then click Next and Finish.
Now the project appears in the left column of Eclipse. I right click it and select New->File and name it main.cpp
The main window of Eclipse opens the file main.cpp and I write inside:
#include <iostream>
int main() {
std::cout << "Hello Erjan\n" << std::endl;
return 0;
}
Then I click on Build, it's the hammer icon in the middle of the toolbar. The code compiles and we are ready to launch it!
So, click on Run icon (3 positions right of the Build icon) and you should see the output in the console.
The first step, create new project and try proper toolchains.
Second step, Project >> build project.
Third step, right click on new executive file and run or debug it.