How to make CDT/Eclipse work with C++11 threads? - c++

I tried to test an example of C++11 threads in Eclipse. But I got this message when running the program:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted'
My system: ubuntu + gcc 4.7
Program:
#include <iostream>
#include <thread>
void worker()
{
std::cout << "hello from worker" << std::endl;
}
int main(int argc, char **argv)
{
std::thread t(worker);
t.join();
}
...and yes, I put -std=c++11 and -pthread inside C/C++ Build -> Settings -> Tool Settings -> Cross G++ Compiler -> Miscellaneous -> Other Flags.
Any comments?

The problem was solved by the comment of Jonathan Wakely.
I added -pthread to C/C++ Build -> Settings -> Tool Settings -> Cross G++ **Linker** -> Miscellaneous -> Other Flags and the program worked correctly.
Thank you Jonathan.

To work C++11 std::thread in Eclipse, one needs to give -pthread option while compiling. However that's not enough. In my Ubuntu 14.04, with Eclipse Kepler and g++4.9 below makes it work:
Right click on Project and select 'Properties'
Go to 'C/C++ Build' > 'Settings' > (tab) 'Tool Settings'
First select 'Cross G++ Compiler' > 'Miscellaneous' > 'Other flags';
and add
-pthread after -std=c++11
Second select 'Cross G++ Linker' > 'Libraries';
and add pthread (which is equivalent
to command line -lpthread)
Finally re-compile the project; the error should go.
Also remember that if you use, std::thread then its object must be join() somewhere. Else you may get below runtime error:
terminate called without an active exception

Go to Project > Properties > C/C++ General > Preprocessor include paths, etc > Providers > CDT GCC Builtin Compiler Settings and append -std=c++11 to the compiler specs.
You can also do this for all projects going to Window > Preferences > C/C++ > Build > Settings > Discovery and append -std=c++11 to the CDT GCC Builtin Compiler Settings specs.
${COMMAND} ${FLAGS} -E -P -v -dD -std=c++11 "${INPUTS}"
Project Properties > C/C++ Build > Settings > Tool Settings > GCC C++ Compiler > Miscellaneous > Other flags, add -pthread -std=c++11 -Wl,--no-as-needed:
-c -fmessage-length=0 -pthread -std=c++11 -Wl,--no-as-needed
Project Properties > C/C++ Build > Settings > Tool Settings > GCC C++ Linker > Miscellaneous > Linker flags, add -pthread -std=c++11 -Wl,--no-as-needed
-pthread -std=c++11 -Wl,--no-as-needed

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

Compilation error with gtkmm

I'm trying to compile a simple helloworld programme on eclipse using gtkmm and compiling with mingw32 through mingw32.
I followed these instructions to "install" gtkmm:
https://wiki.gnome.org/Projects/gtkmm/MSWindows
I also read a lot of threads about that topic but couldn't understand why i'm not able to compile this simple program:
#include <gtkmm.h>
int main(int argc, char** argv)
{
auto app = Gtk::Application::create(argc, argv);
Gtk::Window window;
window.set_default_size(600,400);
return app->run(window);
}
Through eclipse IDE i used the
project=>properties=>C/C++build=>settings=>pkg-config
and i enabled gtkmm-3.0.
Then i compile and i get the error:
Info: Configuration "Debug" uses tool-chain "MinGW GCC" that is unsupported on this system, attempting to build anyway.
Info: Internal Builder is used for build
g++ -I/mingw32/include/gtkmm-3.0 -I/mingw32/lib/gtkmm-3.0/include -I/mingw32/include/atkmm-1.6 -I/mingw32/include/gdkmm-3.0 -I/mingw32/lib/gdkmm-3.0/include -I/mingw32/include/giomm-2.4 -I/mingw32/lib/giomm-2.4/include -I/mingw32/include/pangomm-1.4 -I/mingw32/lib/pangomm-1.4/include -I/mingw32/include/glibmm-2.4 -I/mingw32/lib/glibmm-2.4/include -I/mingw32/include/gtk-3.0 -I/mingw32/include/cairo -I/mingw32/include -I/mingw32/include/pango-1.0 -I/mingw32/include/atk-1.0 -I/mingw32/include/cairomm-1.0 -I/mingw32/lib/cairomm-1.0/include -I/mingw32/include/pixman-1 -I/mingw32/include/freetype2 -I/mingw32/include/libpng16 -I/mingw32/include/harfbuzz -I/mingw32/include/glib-2.0 -I/mingw32/lib/glib-2.0/include -I/mingw32/include/sigc++-2.0 -I/mingw32/lib/sigc++-2.0/include -I/mingw32/include/gdk-pixbuf-2.0 -I/mingw32/includ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -o main.o "..\\main.cpp"
..\main.cpp:1:19: fatal error: gtkmm.h: No such file or directory
compilation terminated.
What is strange is that i can see in the compile log that it include everything needed for this program.
i also tried to add in
project=>properties=>C/C++build=>settings=>tool settings=> gcc c++
compiler and mingw linker
the result of pkg-config gtkmm-3.0 --cflags --libs but i get the exact same error.
It makes 2 days that i'm reading and trying the same propositions i found everywhere but coudn't get a result.
So please can someone try to help me to found a way to get this simple example to work.
thank you a lot
(ps: sorry for my mistakes)
I had the same problem, i solved it by manually adding all this path to include (Project Prorepties > C/C++ Build > Settings > GCC C++ Compiler > Includes > Include Paths (-I)):
"C:\msys64\mingw32\include\gtkmm-3.0"
"C:\msys64\mingw32\lib\gtkmm-3.0\include"
"C:\msys64\mingw32\include\glibmm-2.4"
"C:\msys64\mingw32\lib\glibmm-2.4\include"
"C:\msys64\mingw32\include\glib-2.0"
"C:\msys64\mingw32\lib\glib-2.0\include"
"C:\msys64\mingw32\include\sigc++-2.0"
"C:\msys64\mingw32\lib\sigc++-2.0\include"
"C:\msys64\mingw32\include\giomm-2.4"
"C:\msys64\mingw32\lib\giomm-2.4\include"
"C:\msys64\mingw32\include\gdkmm-3.0"
"C:\msys64\mingw32\lib\gdkmm-3.0\include"
"C:\msys64\mingw32\include\pangomm-1.4"
"C:\msys64\mingw32\include\gtk-3.0"
"C:\msys64\mingw32\include\pango-1.0"
"C:\msys64\mingw32\lib\pangomm-1.4\include"
"C:\msys64\mingw32\include\cairo"
"C:\msys64\mingw32\include\gdk-pixbuf-2.0"
"C:\msys64\mingw32\include\cairomm-1.0"
"C:\msys64\mingw32\include\freetype2"
"C:\msys64\mingw32\include\atkmm-1.6"
"C:\msys64\mingw32\include\atk-1.0"
"C:\msys64\mingw32\include\gtkmm-3.0"
"C:\msys64\mingw32\include\glibmm-2.4"
(You shoud replace "C:\msys64\mingw32" with your mingw32 path)
Also add -std=c++11 flag (Project Prorepties > C/C++ Build > Settings > GCC C++ Compiler > Miscellaneous > Other flags) and
glibmm-2.4
gtkmm-3.0
to (Project Prorepties > C/C++ Build > Settings > MinGW C++ Linker > Libraries (-l))

Setting up GTKmm in Eclipse for C++

I am trying to create a GUI using GTKmm in Eclipse, however I can't get the configuration working. For reference, here is my actual code:
#include <gtkmm.h>
using namespace std;
int main(int argc, char **argv) {
Gtk::Main kit(argc,argv);
Gtk::Window frmMain;
kit.run(frmMain);
return 0;
}
These are the settings I have entered under the GCC C++ Compiler section.
All options:
-I/usr/include/gtkmm-3.0 -I/usr/include/gdkmm-3.0 -I/usr/include/gtk-3.0 -O0 -g3 -Wall
and for Command Line Pattern:
${COMMAND} `pkg-config gtkmm-3.0 --cflags` ${FLAGS} ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS}
Finally, under the GCC G++ Linker section, I have this entered for the Command Line Pattern:
${COMMAND} `pkg-config gtkmm-3.0 --libs` ${OUTPUT_FLAG} ${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} ${FLAGS}
Here is a link to my compile log:
http://pastebin.com/tfqv97w6
I can't put this in the comments they are too restrictive. These are the steps I have just gone through to make a gtkmm program in my eclipse. I followed the procedure twice and it worked both times.
Create Project:
File -> New -> C++ Project
Project Type: Executable -> Hellow World Gtkmm Project
Toolchains: Linux GCC
Project Properties:
C/C++ Build -> Settings -> Pkg-config -> gtkmm-3.0
See if that works for you.
I have a brief article about this (with screenshots), see it here:
http://www.hamedandobaideh.com/index.php/9-uncategorised/73-gtkmm-with-eclipse
I performed two test. One with your source instance and one with an instance from a simple file from the site. Both worked flawlessly the first time using the steps below.
A Hello World example from the gtkmm website with Success:
https://git.gnome.org/browse/gtkmm-documentation/tree/examples/book/helloworld
First I installed the pkg-config Eclipse Plugin:
https://code.google.com/p/pkg-config-support-for-eclipse-cdt/
Then:
Created a new empty C++ project in Eclipse
Added three files to the project (helloworld.cpp, helloworld.h,
main.cc)
Added pkg-config-support to the project: [(right click on Project) -> Properties -> C++
Build -> Settings -> Pkg-config -> (checkmark) gtkmm-3.0 -> OK
From Eclipse I Cleaned, then Build all, then was able to Run the
example.

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.

Configure Eclipse CDT to use g++

I have cygwin installed, and I want to use Eclipse with CDT for development under Windows 7. However, I get following error:
**** Build of configuration Default for project hello_cpp ****
make all
g++ -O2 -g -Wall -fmessage-length=0 -c -o hello_cpp.o hello_cpp.cpp
process_begin: CreateProcess(C:\cygwin\bin\g++.exe, g++ -O2 -g -Wall -fmessage-length=0 -c -o hello_cpp.o hello_cpp.cpp, ...) failed.
make (e=5): Access denied.
make: *** [hello_cpp.o] Error 5
**** Build Finished ****
I'm able to use g++ as standalone compiler.
cygwin /bin folder is
added to path.
After googling I found out that C:\cygwin\bin\g++.exe is a cygwin symbolic link and Windows doesn't understand it and I need to point to the g++-3 location directly. How do I do it?
I think you've done something wrong and need to start over again. Just installed Cygwin and Eclipse CDT (Indigo) on my Windows 7 and all works fine and auto-magicaly for me.
Here's what I did and I think you need to do:
Get the latest Cygwin (yes, get it again! get rid of the old one just to be sure)
During the installation make sure to select gcc, gcc-g++ and make (I additionally installed couple of other things like gcc4, w32api but it's optional)
Start Cygwin terminal to init all configuration files, etc. See if g++ executes and close the terminal.
Add C:\cygwin\bin (or wherever else you installed it) to your Environment PATH variable
Get Eclipse CDT, extract it somewhere and start it up.
Go to File -> New Project -> C++ Project and select Hello World C++ Project. You should see the Cygwin GCC in the Toolchains list.
Create the Project, build and run it!
Done!
Build output:
**** Build of configuration Debug for project TestApp ****
make all
Building file: ../src/TestApp.cpp
Invoking: Cygwin C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/TestApp.d" -MT"src/TestApp.d" -o "src/TestApp.o" "../src/TestApp.cpp"
Finished building: ../src/TestApp.cpp
Building target: TestApp.exe
Invoking: Cygwin C++ Linker
g++ -o "TestApp.exe" ./src/TestApp.o
Finished building target: TestApp.exe
**** Build Finished ****
You can go to
Project Properties Page > C / C++ Build > Settings > Tool Settings
And change the command as you want. Documentation here.
Refer this link, it shows how to setup eclipse for native development with ease. everything is done in eclipse except setting environment variables.