Curly brace uniform initilaization not recognised in Eclipse in c++ - c++

I am learning C++ and I am using Eclipse as an IDE. I downloaded the latest version for Mac (Version: 2018-12 (4.10.0)). I am trying uniform initialization but Eclipse gives me an error.
Here is the code:
#include <iostream>
int main()
{
int x{ 5 };
std::cout << x;
return 0;
}
If I type instead:
int x(5);
or
int x=5;
It works!
The error shown is the following:
Building file: ../src/HelloWorld2.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/HelloWorld2.d" -MT"src/HelloWorld2.o" -o "src/HelloWorld2.o" "../src/HelloWorld2.cpp"
../src/HelloWorld2.cpp:5:10: error: expected ';' at end of declaration
Any ideas?

I couldn't find how to make Eclipse compatible for C++11, but then I found a website (took a while). Here is the solution.
On the project explorer window, right-click your project and choose properties at the bottom of the menu.
Go to "C/C++ Build" on the left, expand the options by clicking the small arrow and then select 'settings'
On the right side of the window, there is a list of settings. Select the 'Miscellaneous' one.
There will be a field called 'Other flags' to the right of that list. Add the following (put a space at the end of the current text).
"-std=c++0x"
That's it!

Related

Unexpected result when initializing variable in C++ using curly braces

I'm using atom to practice C++ (I'm very new). I just learned to initialize variables like the following:
#include <iostream>
using namespace std;
int main() {
int myInt {};
return 0;
}
When I build and run the previous code in codelite I receive no errors. However, if I compile my atom file dailyPractice10.cpp using my MacBook terminal (zsh) I get the following error:
dailyPractice10.cpp:7:12: error: expected ';' at end of declaration
int myInt {};
^
;
1 error generated.
I'm using the following command to compile it on terminal:
g++ -o dailyPractice10 dailyPractice10.cpp (compiles)
./dailyPractice10 (runs program)
Does anyone have any feedback why this code runs in codelite but doesn't compile in terminal?
Because this feature is added from c++11.
if you will like to try below command.it will work.
$ g++ -std=c++0x -o dailyPractice10 dailyPractice10.cpp
The key to fixing this issue is to set the C++11 (or above) standards while building your code.
In the console tab of the IDE, the following output is generated before the error. Notice that no standard is being defined while building the code:
make all
Building file: ../1.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"1.d" -MT"1.o" -o "1.o" "../1.cpp"
We need to add the --std=c++1x flag to the g++ command. The following solution is for the ones using the Eclipse IDE and the MacOSX C++ compiler:
Right click on the project from the "Project Explorer".
Go to Properties > C/C++ Build > Settings.
Under the "Tool Settings" tab, find "GCC C++ Compiler" > "Miscellaneous"
In the "Other Flags" text box, edit the text such that it looks like:
-std=c++17 -c -fmessage-length=0
If you intend to use any other c++ standard, replace "c++17" with the standard of your choice ( eg. c++20).
Apply Changes.
Run Clean, and the Build again.
you should try this to compile the Code
g++ -std=c++20 -o dailyPractice10 dailyPractice10.cpp

Eclipse shows error, but project compiles and executes

I'm working in a Eclipse MARS C++ project. The following code compiles and executes perfectly even though Eclipse keeps showing some errors.
The code:
#include <iostream>
#include <ratio>
#include <chrono>
using namespace std;
int main()
{
typedef std::chrono::duration<int> seconds_type;
typedef std::chrono::duration<int,std::milli> milliseconds_type;
typedef std::chrono::duration<int,std::ratio<60*60>> hours_type;
hours_type h_oneday (24);
seconds_type s_oneday (60*60*24);
milliseconds_type ms_oneday (s_oneday);
seconds_type s_onehour (60*60);
hours_type h_onehour (std::chrono::duration_cast<hours_type>(s_onehour));
milliseconds_type ms_onehour (s_onehour);
std::cout << ms_onehour.count() << "ms in 1h" << std::endl;
}
The errors:
Symbol 'duration' could not be resolved.
Type 'std::milli' could not be resolved.
Symbol 'ratio' could not be resolved.
Symbol 'duration_cast' could not be resolved.
Method 'count' could not be resolved.
Invalid overload of 'std::endl'.
I think all the errors are about something with the includes.
The message when building:
Invoking: Cross G++ Compiler
g++ -D__GXX_EXPERIMENTAL_CXX0X__ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -MMD -MP -MF"src/Teste.d" -MT"src/Teste.o" -o "src/Teste.o" "../src/Teste.cpp"
Finished building: ../src/Teste.cpp
Building target: Teste
Invoking: Cross G++ Linker
g++ -o "Teste" ./src/Teste.o
Finished building target: Teste
I've already put -std=c++11 on PROJECT > PROPERTIES > C/C++ BUILD > SETTINGS > MISCELLANEOUS.
I've already put __GXX_EXPERIMENTAL_CXX0X__ on C/C++ GENERAL > PATHS AND SYMBOLS > SYMBOLS too.
I've already cleaned and rebuild it, closed and reopened it (project and eclipse), but nothing happens. This is a 1 file project, how can Eclipse find 8 errors in it and still compile and execute it? I think its just the editor messing with me. Can anyone help me to be free from the errors?
Don't trust Eclipse, or Visual Studio or any other IDE for that matter. The parser they use to give you "red squiggles" in the editor is not the real compiler. It has different bugs, it has different levels of standard compliance, it has very little time to parse your code (since it's interactive).
It will give false results.
The 'in-editor' parsers are nice, helpful & convenient tools. But they are not the compiler proper and they should not be interpreted as more than helpful hints.

Code Building Error in Eclipse CDT for Player/Stage simulator

I am using Player/Stage for my thesis work. But, I am getting undefined reference error messages while I want to build this code in Eclipse CDT-
#include <iostream>
#include <playerc++.h>
#include <playerclient.h>
#include <clientproxy.h>
using namespace std;
int main()
{
PlayerClient robot("localhost",6665);
return 0;
}
The error message is:
08:40:02 **** Build of configuration Debug for project firstTest ****
make all
Building file: ../src/firstTest.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/local/include/player-3.0/libplayerc++ -include/usr/local/include/player-3.0/libplayerc++/playerc++.h -include/usr/local/include/player-3.0/libplayerc++/clientproxy.h -include/usr/local/include/player-3.0/libplayerc++/playerc++config.h -include/usr/local/include/player-3.0/libplayerc++/playerclient.h -include/usr/local/include/player-3.0/libplayerc++/playererror.h -include/usr/local/include/player-3.0/libplayerc++/utility.h -O0 -g3 -Wall -c -fmessage-length=0 `pkg-config --cflags playerc++` -MMD -MP -MF"src/firstTest.d" -MT"src/firstTest.d" -o "src/firstTest.o" "../src/firstTest.cpp"
Finished building: ../src/firstTest.cpp
Building target: firstTest
Invoking: GCC C++ Linker
g++ `pkg-config --libs playerc++` -o "firstTest" ./src/firstTest.o
./src/firstTest.o: In function `main':
/home/nafees/workspace/firstTest/Debug/../src/firstTest.cpp:19: undefined reference to `PlayerCc::PlayerClient::PlayerClient(std::string, unsigned int, int)'
/home/nafees/workspace/firstTest/Debug/../src/firstTest.cpp:19: undefined reference to `PlayerCc::PlayerClient::~PlayerClient()'
collect2: error: ld returned 1 exit status
make: *** [firstTest] Error 1
08:40:02 Build Finished (took 514ms)
I have followed this nice tutorial posted by Jenny- http://yorkroboticist.blogspot.com/2010/03/playerstage-on-eclipse.html
In this link you will get the snapshots of my Eclipse Project Properties:
https://www.dropbox.com/sh/9hcmditufu0lt6v/AAChqD1-FMM9FhvTWpuOGMQ-a?dl=0
I have tried a lot of ways to remove this error. but, all in vein. Please help me to continue my thesis.
With thanks
Nafees
I have figured out the "undefined reference" problem.....
If anyone wants to use eclipse CDT for stage/player simulation, please follow these steps. I am assuming that you have installed the Player/Stage properly.
Go to Project Properties.
Expand C/C++ Build.
Click on Settings.
If you want to build C++ code, follow these steps-
Under GCC C++ Compiler, click on Miscellaneous tab.
There is a box named "Other flags". Paste this line(with ` symbols) on the box-
`pkg-config --cflags playerc++`
caution: Never edit the existing lines in the box. Just paste the above line beside the existing line with an space. And keep in mind ' and ` is not same.
Then, go to includes/directories(name may vary according to eclipse version) tab. There is an "include paths(-I)" box. click on add button and navigate to- Computer > usr > local > include > Player- x.x and press ok.
If you want to build C code, Skip number 4 instruction and follow these steps instead-
Under GCC C Compiler, click on Miscellaneous.
There is a box named "Other flags". Paste this line (with ` symbol) on the box-
`pkg-config --cflags playerc`
caution: Never edit the existing lines in the box. Just paste the above line beside the existing one it with an space. And keep in mind ' and ` is not same
Go to includes/directories(names may vary according to eclipse version) tab. There is an "include paths(-I)" box. click on add button and navigate to- Computer > usr > local > include > Player- x.x and press ok.
Now, The most important steps come here. Under GCC C++ Linker, click on Miscellaneous tab. There is a box named "Linker flags".
if you are using C++, paste the following line-
`pkg-config --libs playerc++`
If you are using C, paste the following line instead of the above-
`pkg-config --libs playerc`
Now, go to Libraries tab and there you will see "Libraries (-l)" box. Press add button and
If you are using C++, paste the line-
playerc++
If you are using C, paste the following line instead of the above-
playerc
Finally, there is a box named "Library search path (-L)" beneath. Click on add button and navigate to- Computer > usr > local > lib and click ok.
Thats it....... Now try to compile and build an example code. :) :) :)

How to Enable C++11 Features in Codelite

The following code compiles and runs in Xcode 5 and in Visual Studio 2013. I am interested in trying out Codelite, but Codelite will not compile the following program (a problem since I am working with scoped enums in my project). As far as I understand it, Codelite is using the same compiler as Xcode.
Is the code valid per C++11? Why is Codelite unable to compile it?
#include <iostream>
namespace abc
{
namespace xyz
{
enum class SampleEnum
{
SomeValue = 0,
SomeOtherValue = 1
};
}
}
int main(int argc, char **argv)
{
abc::xyz::SampleEnum e = abc::xyz::SampleEnum::SomeValue;
return 0;
}
Here is the build output from Codelite. In case it's garbled, it's pointing to the word "SampleEnum" in the instantiation of the variable and saying "expected a class or namespace".
/bin/sh -c 'make -j8 -e -f Makefile'
----------Building project:[ ClangTest - Debug ]----------
codelite-cc /usr/bin/clang++ -c "/Users/xxx/Desktop/Test/ClangTest/main.cpp" -g -O0 -Wall -o ./Debug/main.cpp.o -I. -I.
/Users/xxx/Desktop/Test/ClangTest/main.cpp:7:8: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions]
enum class SampleEnum
^
/Users/xxx/Desktop/Test/ClangTest/main.cpp:17:40: error: expected a class or namespace
abc::xyz::SampleEnum e = abc::xyz::SampleEnum::SomeValue;
~~~~~~~~~~^
1 warning and 1 error generated.
make[1]: *** [Debug/main.cpp.o] Error 1
make: *** [All] Error 2
2 errors, 1 warnings
It is necessary to pass -std=c++11 to the compiler to enable C++11 features. Here are the steps to do so in Codelite:
Right click on the project in the workspace view.
Select Settings near the bottom of this pop-up menu. Common Settings->Compiler->C++ Compiler Options
Click into the semicolon delimited list of compiler switches to reveal elipses and click on the elipses.
Click the checkbox for -std=c++11
If you are using C++11 extensions, compilers want it to be flagged. Without it they may throw warnings and errors. That's because some of C++11 changes are not backward-compatible, e.g. the use of auto.
For example, in gcc you should have
gcc -std=c++11
Check if your compiler shouldn't have such parameter as well!
I suppose this is because that your default std version is not c++11. To change to c++11, if you are using your terminal, you should type in the following command:
g++ yourfile.cpp -std=c++11

Error: Range-based 'for' loops are not allowed in C++98 mode

So I'm following the tutorials on this page:
http://www.cplusplus.com/doc/tutorial/control/
But I'm having trouble doing a range/based for loop. I found this page:
GNU GCC compiler updatingThe answer there says I should open "Project" and "Properties". But when I try that, the "Properties" option is grayed out with no explanation:
http://imageshack.com/a/img571/4371/xd1x.png
So.. how can I activate range/based for loops?
Pass -std=c++11 flag to the compiler. Certainly GCC should be fresh enough (>=4.7) to support all these modern standards. For CodeBlocks 13.12: Settings -> Compiler -> Tab "Compiler Flags" -> Option "Have g++ follow the C++11 ISO C++ [-std=c++11]"
The above given solution of using -std=c++11 didn't work for me.
This is the target and version detail of my compiler.
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
Target: x86_64-linux-gnu
When I tried, this is what happened.
$ g++ -std=c++11 program.cpp
cc1plus: error: unrecognized command line option ‘-std=c++11’
This solved the problem for me.
$ g++ -std=c++0x program.cpp
In Dev-Cpp 5.11 Simply you can click on Tools>Compiler Options>Settings>Code Generation>(and in last option)Language standard(-std) select from dropdown (ISO C++ 11).
If you faced this error in Code::Blocks, this might help you -
Click on Settings -> Compiler -> Compiler Settings -> Compiler Flags
Under the "General" section, check [✓] the box beside :
Have g++ follow the C++11 ISO C++ language standard [-std=c++11]
Both of these:
g++ -std=c++11 -o test_executable test_source.cpp
g++ -std=c++0x -o program program.cpp
worked for me.
Only thing to do after compiling is to execute the test_executable (in the first case) as ./test_executables or program (in the second case) as ./program.
Using the above solution
g++ -std=c++0x program.cpp
works. However, the command needs to be modified slightly in order to run the program with the common command:
./program
I used
g++ -std=c++0x -o program program.cpp
and all worked perfectly.
If you are using QT5.5, you can achieve it by adding following lines in your .pro file.
CONFIG += c++11
The best solution is doing this, in Dev C++:
Go to the "Tools" Option
Select "Compiler Options"
Click "Settings"
Click "Code Generation"
Go to "Choose Language" and select "ISO C++11"
Then your problem is resolved.
in first time, if you have Dev-C++
C:\Program Files (x86)\Dev-Cpp\MinGW64\bin, you must add this route to the path
then use the next command in cmd.
g++ -std=c++11 -o outprogram code_source.cpp
this command in your project directory.