Netbeans 7.0 compiler error - c++

I am using Netbeans 7.0 and get this error when I try to compile, and debug:
make: *** [.validate-impl] Error 127
BUILD FAILED (exit value 2, total time: 281ms)
I set my environment variables (within Windows) to C:\cygwin\bin
Within Netbeans my build tools are of the Cygwin family. The C compiler is Gcc, C++ compiler is G++, Assembler is as.exe, make command is make.exe, and debugger is gdb.exe. They're all located within C:\cygwin\bin\FILENAMEHERE
And finally, my source code:
#include <iostream>
int main ()
{
std::cout << "Enter two numbers:" << std::endl;
int v1, v2;
std::cin >> v1 >> v2;
std::cout << "The sum of " << v1 "and " << v2 << "is" << v1 +v2 << std::endl;
return 0;
}
Any suggestions?

I've had a LOT of problems with the 7.0 C++ version. It keeps destroying the auto generated make files. I removed it and downgraded. The 6.9.1 version is still available and seems to work much better.
p.s.
If you're going to be doing QT development it matters which compiler chain you choose. You want to look for "mingw tdm dw2". The SJLJ version of mingw (which is the default) does not work with the released QT binary libraries.

Related

C++ not printing emojis as expected

I have the following extract of a program
int main(){
cout << "1) ✊\n";
cout << "2) ✋\n";
cout << "3) ✌️\n";
}
But at the time I run it I get strange texts like the following
====================
rock paper scissors!
====================
1) 
2) 
3) ԣÅ
This seems not to be related to my terminal but instead to a compilation result because if I run echo ✊ it shows as expected.
See example below
I'm currently using the following compilation commands and compiler version
g++ *.cpp -o rock_paper_scissors.exe
g++.exe (Rev9, Built by MSYS2 project) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
Finally, note that it was working before as expected, but at some point, it stopped working, I noticed after I used system("pause") which I'm guessing may have changed something on the compilation configurations as this is a Windows-only command, I delete such piece of code and still having the issue.
You can see the rest of the code here: https://github.com/guillene/RockPaperScissors
If your terminal font supports emojis and you don't want to write much code (like switching from cout to wcout), you can use the windows api function below.
#include <windows.h>
#include <iostream>
int main(){
SetConsoleOutputCP(CP_UTF8);
std::cout << "1) ✊\n";
std::cout << "2) ✋\n";
std::cout << "3) ✌️\n";
return 0;
}
see: https://learn.microsoft.com/en-us/windows/console/setconsoleoutputcp

G++ Compiles, Executable Won't Run

I've been using a text editor and g++ to compile and run programs for a while, but for some reason, it stopped working today.
It will compile without errors but when I try to run the executable, it doesn't do anything. No errors, no output. Nothing. This is the code I'm trying to run.
#include <iostream>
#include <fstream>
int main(){
// Initializes variable.
int coordinatePair;
// Creates an object to use for the file.
std::ofstream fileReader;
// Initiates a for-loop to get the value of each variable.
for(int i = 0; i == 5; i++){
std::cout << "Please enter the x-coordinate of your coordinate pair. " << std::endl << "You have " << 5 - i << " pairs left to enter." << std::endl;
std::cin >> coordinatePair;
// Opens the file.
fileReader.open("points.txt");
// Writes the user's values to the file.
fileReader << coordinatePair << std::endl;
// Closes the file.
fileReader.close();
}
}
In the terminal, I created a directory to the file location...
cd ~/file location
And then I compiled it.
g++ points_out.cpp -o points_out
And I tried to run it.
./points_out
And nothing happens. No error messages, no output, no nothing. While the code isn't exactly efficient (I'm probably better off opening and closing the file outside of the for-loop.) it should still run. I tried putting in incorrect code to see what would happen, and it gave me the proper error codes. I also tried
g++ -W -Werror points_out.cpp
...and that didn't give me any error codes either. I tried making a new directory to a different .cpp file I had compiled and ran earlier this week and it worked just fine. For some reason, this one just will not run. And I'm confident that it is compiling because the executable is being created. I'm also confident that it is not running, because the text file is not being created.
I got this thing to compile successfully and run once and it created the text file when it did. This is the first time I've run into issues with g++. I've looked everywhere for a solution but most of the people who had this problem just weren't doing the
./points_out
part or had issues with MingW or needed to uninstall Avast. None of these are the issue I have so I'm at a loss.
Also, not sure if this will help, but running
g++ --version
gets me the following output:
g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
The behavior is normal for a program doing nothing and completing successfully.
The C++ for-loop evaluates the loop condition before entering the loop body. When the loop body is completed, the optional post-loop expression is evaluated and the program returns to the conditional check.
So your code assigns the value 0 to i and then tests to see if i == 5 before allowing entry into the loop.
It's not entirely clear what you were intending, but you probably mean't
for (int i = 0; i < 5; i++)
We can demonstrate that quickly with this piece of code:
#include <iostream>
int main() {
std::cout << "original loop:\n";
for (int i = 0; i == 5; i++)
std::cout << i << '\n';
std::cout << "corrected loop:\n";
for (int i = 0; i < 5; i++)
std::cout << i << '\n';
}
Live demo: http://ideone.com/O0Ul2z
-- Addendum --
Most programmers would have thought to add something like a no-frills, no-conditions
std::cout << "hello world\n";
at the top of their program to verify that it wasn't running vs not working.

Enabling `-std=c++14` flag in Code::Blocks

I have installed Code::Blocks for Windows and want to compile C++14 code like generic lambdas but the binary version of Code::Blocks that I've installed from codeblocks.org doesn't support the flag -std=c++14.
How do I update the compiler and enable -std=c++14 flag for Code::Blocks?
To compile your source code using C++14 in Code::Blocks, you, first of all, need to download and install a compiler that supports C++14 features.
Here’s how you can do it on Windows:
Download MinGW from here (particular build) or from official site to choose options
Extract it to for example: C:\ (result will be C:\MinGW)
Open Code::Blocks
Go to Settings => Compiler.
Go to “Toolchain Executables”.
In the top field “Compiler’s installation directory”, change the directory to the one where you extracted the compiler. E.g C:\MinGW.
Change all the necessary files under “Program Files” to match the files under C:\MinGW\bin:
Before you hit “OK”, go to the leftmost tab “Compiler settings”.
Select “Compiler Flags”.
For simplicity, right-click in the list somewhere and select “New Flag”:
Type in the following and click "OK", and tick the box of the flag you just created:
Lastly, you need to specify the debugger path. Go to "Settings" => "Debugger", click "Default" on the left-hand side and enter the new full path of the executable:
Now, try to compile a program with C++14 features:
#include <iostream>
#include <string>
using namespace std;
auto main() -> int
{
auto add_two([](auto x, auto y){ return x + y; });
cout << add_two("I"s, "t"s) << " works!" << endl;
}
May a humble newbie make one small suggestion? A small modification to test C++14 code, to allow resulting .exe file to be run independently of the IDE it was created in, slightly modified test program follows:
#include <iostream>
#include <string>
using namespace std;
auto main() -> int
{
auto add_two([](auto x, auto y){ return x + y; });
cout << add_two("I"s, "t"s) << " works!" << endl;
cout << "press enter to continue..." << endl;
cin.ignore(10, '\n');
cin.get();
}
Thank you all, peace to all fellow coders, especially Igor Tandetnik.

Error When Trying to Compile in Eclipse (MacOSX)

I am learning Eclipse and C++ and am working on an extremely simple program. I can't build the program now. I just recently installed HomeBrew so I don't know if that has something to do with it. Here is my code (it's from a simple tutorial I'm working off of so I know the code works because I've seen it work on the tutorial):
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Ask the user to input a word while the word length < 5
string word = "";
do
{
cout << "Enter a word that has at least 5 characters: " << endl;
cin >> word;
}while(word.size() < 5);
// Ask the user to input a character
char searchCh = '0';
cout << "Enter a character and the program will tell" <<
" you how many times it appears in the word " << word << "." << endl;
cin >> searchCh;
int counter = 0;
// Iterate over the word
for(int i = 0; i < word.size(); i++)
{
// Get a character
char ch = word.at(i);
// If the character matches the character we're looking for
if(searchCh == ch)
{
// Increment a counter
counter++;
}
}
// Output the number of times the character was found in the word.
cout << "The number of " << searchCh << "'s in the word " << word << " is " << counter << "\n";
return 0;
}
The error I get when build is this:
g++ -o "Program 5" ./Program5.o
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Program 5] Error 1
I've seen several other similar question on here about going to 'Project' and 'Properties' but I don't know what settings exactly are supposed to be updated to get this working. Also, some of the other answers mentioned that a main() function is needed to fix this issue, but I obviously have one in this code. It was working fine last week when I was using Eclipse on other small projects so I'm not sure what changed.
For further information:
When I first the set the project up, it is set as an 'Empty Project' project type with 'MacOSX GCC' as the toolchain. I then created a source file (with the .cpp extension) within the project, wrote the code, and then that's where I'm at.
Any help would be greatly appreciated. Thanks!
Try resetting up your project. Sometimes installing buildchains can confuse eclipse and make it use the wrong libraries - especially if it is expecting another version.
What I think happened here is that eclipse confused was using a buildpath that did not contain C++ files (due to an upgrade moving them?) but instead contained C files, which don't link with C++ under normal circumstances.
As also mentioned by another commenter, this could also be the result of an initial failed build not being cleaned up for some reason.
Sometimes with eclipse, the solution is the same as with Windows: Restart it, redo whatever went wrong again.

Extremely slow std::cout using MS Compiler

I am printing progress of many iterations of a computation and the output is actually the slowest part of it, but only if I use Visual C++ compiler, MinGW works fine on the same system.
Consider following code:
#include <iostream>
#include <chrono>
using namespace std;
#define TO_SEC(Time) \
chrono::duration_cast<chrono::duration<double> >(Time).count();
const int REPEATS = 100000;
int main() {
auto start_time = chrono::steady_clock::now();
for (int i = 1; i <= REPEATS; i++)
cout << '\r' << i << "/" << REPEATS;
double run_time = TO_SEC(chrono::steady_clock::now() - start_time);
cout << endl << run_time << "s" << endl;
}
Now the output I get when compiled with MinGW ("g++ source.cpp -std==c++11") is:
100000/100000
0.428025s
Now the output I get when compiled with Visual C++ Compiler November 2013 ("cl.exe source.cpp") is:
100000/100000
133.991s
Which is quite preposterous. What comes to mind is that VC++ is conducting unnecessary flushes.
Would anybody know how to prevent this?
EDIT: The setup is:
gcc version 4.8.2 (GCC), target i686-pc-cygwin
Microsoft (R) C/C++ Optimizing Compiler Version 18.00.21005.1 for x86
Windows 7 Professional N 64-bit with CPU i7-3630QM, 2.4GHz with 8.00GB RAM
std::cout in MSVC is slow (https://web.archive.org/web/20170329163751/https://connect.microsoft.com/VisualStudio/feedback/details/642876/std-wcout-is-ten-times-slower-than-wprintf-performance-bug-in-c-library).
It is an unfortunate consequence of how our C and C++ Standard Library
implementations are designed. The problem is that when printing to the
console (instead of, say, being redirected to a file), neither our C
nor C++ I/O are buffered by default. This is sometimes concealed by
the fact that C I/O functions like printf() and puts() temporarily
enable buffering while doing their work.
Microsoft suggests this fix (to enable buffering on cout/stdout):
setvbuf(stdout, 0, _IOLBF, 4096)
You could also try with:
cout.sync_with_stdio(false);
but probably it won't make a difference.
avoid using std::endl but use instead "\n". std::endl is supposed to flush according to the standard.