I've just installed MinGW with mingw32-base and mingw32-gcc-g++ on Windows 10 and added "C:\MinGW\bin\" to the system path and I can run gcc and g++ from the command prompt, but when I try to compile a .cpp file with g++ helloworld.cpp -o helloworld.exe it creates helloworld.exe and after about 2 seconds it deletes helloworld.exe. If I try to run helloworld.exe from the prompt before it deletes it, it gives me "Access is denied.".
Here is helloworld.cpp:
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
But even if I use other code it does the same.
Related
I'm getting started learning c++ project building on linux with simple hello world code.
#include <iostream>
int main(){
std::cout<<"hello,world"<<std::endl;
return 0;
}
When I try to run command
g++ hello.cpp
It fails with:
/opt/rh/devtoolset-6/root/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/ld: cannot find /usr/lib64/libmvec_nonshared.a
/opt/rh/devtoolset-6/root/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/ld: cannot find /lib64/libmvec.so.1
But when I try
gcc -lstdc++ hello.cpp
The compilation is successfully finished and generated executable file a.out.
So what is problem here with g++?
I am using Qt to create a project which compiles external c++ project which has one .cpp file or multiple .cpp and .h files.
Using system("command") I can compile and run ONE .cpp file but I could not figure out how to compile multiple files:(
I tried different ways:
system("cd the_files_directory") then system("g++ *.h *.cpp -o execute") but system("dir") told me it didn't change the directory.
system("g++ the_files_directory/*.h the_files_directory/*cpp -o execute)
system("g++ -fworking-directory the_files_directory/*.h the_files_directory/*cpp -o execute")
All work in normal windows or Linux terminal but just didn't work in system(command).
Thanks in advance
system("cd the_files_directory");
system("g++ *.h *.cpp -o execute");
The first line spawns a new process, a shell, which executes the command "cd the_files_directory", and exits. Your processes working directory is unaffected by the child process. So you are still in your original directory when you execute the g++ command.
Also, this command tries to compile header (.h) files, which would might cause g++ to fail, but is probably just redundant.
system("g++ the_files_directory/*.h the_files_directory/*cpp -o execute)
Again, you're trying to compile header (.h) files, which might cause g++ to fail, but even if it works, it is creating execute in your current directory and not in the_files_directory. You probably want
system("g++ the_files_directory/*.cpp -o the_files_directory/execute");
Your last variant
system("g++ -fworking-directory the_files_directory/*.h the_files_directory/*cpp -o execute")
Is the same command with debugging information about directories, so it has the same problems as above.
Given ('tfd' for 'the_files_directory'):
tfd/foo.cpp
#include <iostream>
#include "foo.h"
void foo() {
std::cout << "foo\n";
}
tfd/foo.h
extern void foo();
tfd/test.cpp
#include "foo.h"
int main() {
foo();
}
And at the top level, comp.cpp
#include <stdlib.h>
int main() {
system("g++ tfd/*.cpp -o tfd/execute");
}
I did the following:
osmith#vbx:~/tmp$ g++ -Wall -std=c++14 comp.cpp -o compiler
Execute the compiler:
osmith#vbx:~/tmp$ ls tfd
foo.cpp foo.h test.cpp
osmith#vbx:~/tmp$ ./compiler
osmith#vbx:~/tmp$ ls tfd
execute foo.cpp foo.h test.cpp
Now test the resulting executable:
osmith#vbx:~/tmp$ tfd/execute
foo
-Guys! I found my own solution last night!
-Here are my steps which will surely work after so much experiment for different ways by myself before you guys posts!
-Step1:
-Move my whole project out from Qt project location and remove .pro and other files generated by Qt and just keep my own .cpp files and .h files. I was doubting if Qt is too protective because I tried to move my project to the "current location" which is Qt project build folder by using
-system("copy project_path/xxx.cpp xxx.cpp");
this didn't give an error but said
-(0) files are copied.
-So I decided to get rid of Qt lol
-Step2:
-cd the folder where I put my project in Windows Command Prompt
$cd project_path
-Step3:
-compile my own project in Windows Command Prompt
project_path$g++ *.h *.cpp -o execute
compile command: g++ project_path\main.cpp project_path\pair.cpp project_path\map.cpp project_path\pair.h project_path\map.h
(i tried to compile not in its own directory but somehow doesn't work:(( )
ps: inside my project, I have these codes to compile and run the external program:
void compile(){
string s = "g++ "
"project_path\\main.cpp "
"project_path\\pair.cpp "
"project_path\\map.cpp "
"project_path\\pair.h "
"project_path\\map.h";//only this works!!!!
char *compile_command = new char[s.length()];
compile_command[0] = '\0';
strcat(compile_command, s.c_str());
cout << "compile command: " << compile_command << endl;
system(compile_command);
}
-I tried to add "-o execute" to the end of string s but it won't work correctly later. and I tried to set s = "g++ project_path/*.h project_path/*cpp" but it didn't work either:((
void run(bool input, string input_path){
if(input){
string s = "a <" + input_path + "> print.txt";
char *execute_command = new char[s.length() + 1];
execute_command[0] = '\0';
strcat(execute_command, s.c_str());
system(execute_command);
cout << "execute_command: " << execute_command << endl;
}else{
system("a > print.txt");
}
}
int main(){
compile();
run(false, "");
}
-Step4:
-run execute.exe
project_path$execute
then a.exe and print.txt will be seen in the project_folder and print.txt showing correct content.
-I also tried to compile and run for some projects requiring input with input.txt. Works fine too!!!
-I will try the solutions posted above too, later! All explanations are very good! Thank all very much! good day :D
When I try to directly run the exe file in windows 7 x64 the system pushed the error message "Incompatible check system information". Both my mac and window7 are x64, so what should I do ?
Try ELLCC:
[~] Richards-Mac-mini% cat main.cpp
#include <iostream>
int main()
{
std::cout << "hello world" << std::endl;
}
[~] Richards-Mac-mini% ~/ellcc/bin/ecc++ -target x86_64-w64-mingw32 -o hello main.cpp
[~] Richards-Mac-mini% file hello
hello: PE32+ executable for MS Windows (console) Mono/.Net assembly
[~] Richards-Mac-mini%
There are binary downloads available for several Linux systems as well as Windows and Mac OS.
I have a basic C++ program in Eclipse CDT:
#include <iostream>
using namespace std;
int main()
{
std::cout << "Hello World!";
}
However, when I try to build it, I get an Exec Format Error. Here is the output produced by the compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o hey.o "..\\hey.cpp"
g++: error: spawn: Exec format error
I am using MinGW Toolchain. I am on 64-bit Windows, and I think that may have something to do with it. Would anyone know how to get this program running?
Edit
Running the exact command in command prompt in the directory where my source file is works just fine, without throwing errors, but it still doesn't work in Eclipse
So, I have fixed this myself by installing the 64-bit version of MinGW (http://sourceforge.net/projects/mingw-w64/). It now compiles and builds noramlly
I have installed the Bloodshed Dev-C++ compiler on my Windows 7 machine. Now how can I compile from the command line?
File main.cpp:
int main()
{
return 0;
}
Compiling:
g++ -Wall main.cpp -o main
The bin folder from MinGW has to be in the path environment variables, and for g++ there are lots of documentation on how to use it. As far as I know, there is not a difference from g++/MinGW and g++ from Linux, so any Linux tutorial will also work on Windows.