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
Related
I have an Eclipse C++ project which initially has first.cpp. Then second.cpp is added and should be linked to the original file. Using Eclipse building tool, I got this output:
make all
Building file: ../src/first.cpp
Invoking: GCC C++ Compiler
g++ -I/home/workspace/first/src -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/first.d" -MT"src/first.o" -o "src/first.o" "../src/first.cpp"
Finished building: ../src/first.cpp
Building file: ../src/second.cpp
Invoking: GCC C++ Compiler
g++ -I/home/workspace/first/src -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/second.d" -MT"src/second.o" -o "src/second.o" "../src/second.cpp"
Finished building: ../src/second.cpp
Building target: first
Invoking: GCC C++ Linker
g++ -L/usr/local/lib -o "first" ./src/first.o ./src/second.o
Finished building target: first
How can I get Eclipse to compile this way?
g++ first.cpp second.cpp -o first
Thank you so much.
============================================================================
I am asking how to make a single binary from multiple source files, not building multiple binaries with multiple source files.
Try using CMake
As per my understanding of your question, you would need to add your source files into CMakeList.txt and then run it. You can make use of this tutorial in doing so.
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.
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:
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).
by accidentally, I linked with --shared flag with a hello world c++ program and got an exe file. but output is segfault when I run it. Can someone tell me the reason behind it?
way to reproduce:
standard c++ hello world problem in eclipse c++.
check the shared flag box in setting--> shared library setting.
build output:
make all
Building file: ../app.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -fPIC -MMD -MP -MF"app.d" -MT"app.d" -o "app.o" "../app.cpp"
Finished building: ../app.cpp
Building target: app
Invoking: GCC C++ Linker
g++ -shared -o "app" ./app.o
Finished building target: app
execution output:
segmentation fault
Thanks
From g++ manual:
--shared
Produce a shared object which can then be linked with other objects to form an executable. Not all systems support this option. For predictable results, you must also specify the same set of options that were used to generate code (-fpic, -fPIC, or model suboptions) when you specify this option.[1]
When you put the --shared option, it means that you don't want an executable, but a shared object.
In your case, you create a shared library and not an executable. This is why you segfault when your launch it.