First of all, I have already checked different solutions:
Threads std::system_error
Threads std::system_error II
Compile multithreading with gcc
Bug gcc multithreading
My environment:
Ubuntu 1404 64bits
gcc 4.8.2
Eclipse IDE for C/C++ Developers
Version: Luna Service Release 1 (4.4.1)
Build id: 20140925-1800
Following these links I managed to compile and run a basic thread program(the code was taken from one of the links in SO, but cannot find it again. If someone sees it, please edit my question to add the reference).
#include <iostream>
#include <thread>
#include <vector>
void func(int tid)
{
std::cout << "Launched by thread " << tid << std::endl;
}
int main()
{
std::vector<std::thread> th;
int nr_threads = 10;
for(int i = 0; i < nr_threads; ++i)
{
th.push_back(std::thread(func, i));
}
for(auto &t : th)
{
t.join();
}
return 0;
}
The command I use to compile it is the following(THIS WORKS and the output file is executable):
g++ --std=c++11 -pthread test.cpp -o test.out
Launched by thread 1
Launched by thread 5
Launched by thread 3
Launched by thread 6
Launched by thread 4
Launched by thread 0
Launched by thread 7
Launched by thread 8
Launched by thread 9
Launched by thread 2
The problem is when I try to setup my eclipse project. I can compile but it cannot produce a valid runnable output.
Compile log:
12:09:45 **** Incremental Build of configuration Debug for project test ****
make all
Building file: ../test.cpp
Invoking: GCC C++ Compiler
g++ --std=c++11 -pthread -D__cplusplus=201103L -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"test.d" -MT"test.d" -o "test.o" "../test.cpp"
Finished building: ../test.cpp
Building target: test
Invoking: GCC C++ Linker
g++ -o "test" ./test.o
Finished building target: test
12:09:46 Build Finished (took 780ms)
I was changing different settings for the builder, dialects... as they say in the links trying to get the same command I can use to compile from the terminal. But no way to create a valid output file from the eclipse. It always shows this error:
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Any idea how to setup eclipse?
Update:
Following the indications of #Dirk I tested in a virtual machine and it works just adding the pthread to the linker libraries.
But for my initial setup it still fails. I made it working after modifying the C/C++ Build settings G++ linker command to g++ --std=c++0x -pthread
So, it seems obvious that my first environment is missing something.
Seems like linking with the pthread library is missing. Add "pthread" under Build->Settings->Tool Settings->GCC C++ Linker->Libraries.
This is what my build log says:
**** Build of configuration Debug for project testpthreads ****
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 --std=c++0x -pthread -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: testpthreads
Invoking: GCC C++ Linker
g++ -o "testpthreads" ./main.o -lpthread
Finished building target: testpthreads
Your code then works and outputs:
Launched by thread Launched by thread Launched by thread 1
Launched by thread 0
Launched by thread 2
Launched by thread 9
Launched by thread 3
Launched by thread 7
Launched by thread 6
4
Launched by thread 8
5
I get the same error as you Without -lpthread
My linker settings:
Related
I am using a new install of cygwin with glut32 libraries on a Windows 7 64-bit machine. I am trying to put together a test program in C++ using the Eclipse IDE. Standard terminal programs and a test GLUT32 program build and run as expected. I am trying to hide the console window on a release build of my test program.
I attempted to use the -mwindows flag in the linker step to suppress the console window, but I get the following:
11:15:04 **** Incremental Build of configuration Release for project Test ****
make all
Building file: ../src/Test.cpp
Invoking: Cygwin C++ Compiler
g++ -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Test.d" -MT"src/Test.o" -o "src/Test.o" "../src/Test.cpp"
Finished building: ../src/Test.cpp
Building target: Test.exe
Invoking: Cygwin C++ Linker
g++ -L"C:\cygwin\lib" -Xlinker -mwindows -shared -o "Test.exe" ./src/Test.o -lglut32 -lglu32 -lopengl32
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../x86_64-pc-cygwin/bin/ld: unrecognised emulation mode: windows
Supported emulations: i386pep i386pe
collect2: error: ld returned 1 exit status
make: *** [makefile:47: Test.exe] Error 1
11:15:05 Build Finished (took 376ms)
I think the supported emulation statement might be a hint. Am I somehow using the wrong compiler? How do a build a finished program without having a console windows pop up in the background?
I can't explain why, but the problem is solved when I use the -mwindows flag in the compilation step as opposed to the linking step. Program builds with no problems and runs without the console.
I can't get the following code to run:
#include <iostream>
int main()
{
std::cout << "Hello World" << std::endl;
return 0;
}
There are no compile errors.
The build output:
18:18:58 **** Incremental Build of configuration Debug for project First Project ****
Info: Internal Builder is used for build
g++ "-Ic:\\mingw\\lib\\gcc\\mingw32\\4.8.1\\include\\c++" "-Ic:\\mingw\\lib\\gcc\\mingw32\\4.8.1\\include\\c++\\mingw32" "-Ic:\\mingw\\lib\\gcc\\mingw32\\4.8.1\\include\\c++\\backward" "-Ic:\\mingw\\lib\\gcc\\mingw32\\4.8.1\\include" "-Ic:\\mingw\\include" "-Ic:\\mingw\\lib\\gcc\\mingw32\\4.8.1\\include-fixed" -O0 -g3 -Wall -c -fmessage-length=0 -o Hello.o "..\\Hello.cpp"
18:18:58 Build Finished (took 166ms)
I get the dreaded "Launch failed. Binary not found." dialog. I've been over google for hours now, but nothing works.
In my project folder, a debug or release folder are created (depending on what I set it to) but it's always empty. No 'binaries' folder is created, as some posts mentioned it should.
No proposed solution worked, and disabling the antivirus didn't solve it.
How do I fix this? Thank you.
EDIT
Eclipse Version Kepler Severive Release 1
Build id: 20130919-0819
CDT Version: 8.3.0.201402142303
I'm using MinGW
EDIT2
Changing the "current builder" in properties>c/c++ build>tool chain editor from CDT internal builder to Gnu Make Builder does generate a makefile.
Output:
13:57:41 ** Auto Build of configuration Debug for project First Project **
make all
Building file: ../Hello.cpp
Invoking: GCC C++ Compiler
g++ -I"c:\mingw\lib\gcc\mingw32\4.8.1\include\c++" - I"c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\mingw32" - I"c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\backward" -I"c:\mingw\lib\gcc\mingw32\4.8.1\include" -I"c:\mingw\include" -I"c:\mingw\lib\gcc\mingw32\4.8.1\include-fixed" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Hello.d" -MT"Hello.d" -o "Hello.o" "../Hello.cpp"
make: * [Hello.o] Error 1
13:57:42 Build Finished (took 881ms)
Which compiler are you using? It appears that you are only compiling your code, not linking it too. You must build and link in order for it to work.
The -c flag only compiles the source file. It doesn't link with the object file into an executable. Add this:
g++ -o "..\\Hello.exe" "..\\Hello.o" && "..\\Hello.exe"
Just remove -c flag from your compile line and change -o Hello.o to -o Hello.exe (guess you use Windows).
Can someone share their compiler settings .. CLANG...toolchain from CodeBlocks ?
Solution
Edit in retrospect
... Can I change the execute mode (chmod) on a particular hard drive partition?
Short term solution ... execute from the home directory
Since I only have comments below I am not sure I can mark an answer.
I will leave my misunderstanding below.
I am on Linux Mint
Partial Success .... Clang does appear to examine and compile code.
Cleaned "test200 - Debug"
-------------- Build: Debug in test200 (compiler: Clang Hand Labor)---------------
clang++ -Wall -fexceptions -g -c "/media/transcend/Seagate Replica/cpp2011/test200/test200/main.cpp" -o obj/Debug/main.o
clang++ -o bin/Debug/test200 obj/Debug/main.o
Output size is 27.32 KB
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings (0 minutes, 0 seconds)
Console Output (typed by Hand .... I cannot copy from tiny console of CodeBlocks)
This is the !!!Error!! ... Just cout << "Hello"
elided
sh: 1: /path/st200 permission denied
returned 126
Compiler Settings in CodeBlocks IDE
C..clang
C++ clang++
Linker clang++
Linker for static ... ar
Resource Compiler ... nothing
make program ...........make
I used Software Manager of Linux for CodeBlocks IDE
There was no CLANG options present ... this fact makes me think I have something old
I copied the GCC Settings and typed clang and clang++
Thanks
Build Log Elided
clang++ -Wall -fexceptions -g -I/usr/lib/llvm-3.2/include elided -Wcast-qual -c "/media/transcend2020/Seagate Replica/cpp2011/test200/test200/main.cpp" -o obj/Debug/main.o
clang++ -o bin/Debug/test200 obj/Debug/main.o -lLLVMAsmParser elided -L/usr/lib/llvm-3.2/lib -lpthread -lffi -ldl -lm
Output size is 51.20 KB
This question already has answers here:
What are the correct link options to use std::thread in GCC under linux?
(5 answers)
Closed 9 years ago.
Update in 2022
C++ 17 & 20 now have built in support for multithreading in the standard library. I would suggest using these rather than using the Linux specific pthread library.
Original Question
I wrote a program to test threads on 64 bit kubuntu linux, version 13.04. Actually I robbed the code from someone else who was writing a test program.
#include <cstdlib>
#include <iostream>
#include <thread>
void task1(const std::string msg)
{
std::cout << "task1 says: " << msg << std::endl;
}
int main(int argc, char **argv)
{
std::thread t1(task1, "Hello");
t1.join();
return EXIT_SUCCESS;
}
I compiled using:
g++ -pthread -std=c++11 -c main.cpp
g++ main.o -o main.out
Then ran:
./main.out
As an aside, when I 'ls -l', main.out shows up in in green text like all executables, but also has an asterisk at the end of its name. Why is this?
Back to the problem in hand: When I ran main.out, an error appeared, which said:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)
Anyone any ideas on how to fix this?
You are not linking pthread properly, try below command(note: order matters)
g++ main.cpp -o main.out -pthread -std=c++11
OR
Do it with two commands
g++ -c main.cpp -pthread -std=c++11 // generate target object file
g++ main.o -o main.out -pthread -std=c++11 // link to target binary
resolution: pass "-static" in miscellanious linker options
---OR---
resolution: Download this MinGW-Version http://sourceforge.net/projects/mingwbuilds/?source=dlp
I am using Eclipse with the MinGW-w64 Toolchain. My GCC version is 4.8
I want to create a application that consists out of 2 Threads. I've allready tried to implement this into my actually application. Thus it didnt work i decided to make a new test app.
Source(main.cpp):
#include <iostream>
#include <thread>
using namespace std;
void test() {
cout << "works.." << endl;
}
int main() {
thread t(test);
t.join();
return 0;
}
Build Process:
21:19:51 **** Build of configuration Debug for project Test ****
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -std=gnu++11 -O0 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
Finished building: ../main.cpp
Building target: Test.exe
Invoking: MinGW C++ Linker
g++ -o "Test.exe" ./main.o -lpthread -lwinpthread -pthread -lpthread
Finished building target: Test.exe
21:19:53 Build Finished (took 1s.863ms)
When running i get the following error:
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
btw: the many -pthread etc. commands come from a couple of trys to make it work.
im running win vista 32 bit
I really appreciate if some one knows how to resolve my problem.
Thanks in advance!
See here for native win32 std::thread implementation that can be added to any C++11 version of MinGW:
https://github.com/meganz/mingw-std-threads