Why won't this C++ program compile and run? [duplicate] - c++

This question already has answers here:
How can I compile and run C/C++ code in a Unix console or Mac terminal?
(19 answers)
Closed 6 years ago.
I tried to run a basic C++ file in the terminal:
#include <iostream>
using namespace std;
int main() {
cont << "This is my first C++ program!";
return 0
}
And then tried to run it in the terminal:
make learningCPP.cpp
make: *** No rule to make target `learningCPP.cpp'. Stop.
And tried:
make learningCPP
make: *** No rule to make target `learningCPP'. Stop.
And tried:
gcc learningCPP.cpp -o learningCPP.out
clang: error: no such file or directory: 'learningCPP.cpp'
clang: error: no input files
Here is the entire Bash/Clang file:
Last login: Mon May 25 07:49:21 on console
make learningCPP.cpp
make: *** No rule to make target `learningCPP.cpp'. Stop.
make learningCPP
make: *** No rule to make target `learningCPP'. Stop.
gcc learningCPP.cpp -o learningCPP.out
clang: error: no such file or directory: 'learningCPP.cpp'
clang: error: no input files
$ g++ -o lab21 learningCPP.cpp
-bash: $: command not found
$ ./lab21
-bash: $: command not found
./learningCPP.cpp
-bash: ./learningCPP.cpp: No such file or directory
./main
-bash: ./main: No such file or directory
$ g++ -o main learningCPP.cpp
-bash: $: command not found
cpp
make learningCPP.cpp
run
How can I fix it?

Judging by the errors, your file either isn't called learningCPP.cpp, or isn't in the directory you're trying to compile it from.
Rename it so it has that name, or change directory to its location, then the build command is
g++ learningCPP.cpp -o learningCPP
not gcc, and with no spurious $ before it. Alternatively, as long as the source file is present in the working directory, you could use make learningCPP.
Once that succeeds, run the program with
./learningCPP
although you'll have to read the new error messages, and use that to figure out how to fix the syntax errors, before it will compile.

Related

How to compile and run a vmmlib test program?

I have successfully installed vmmlib 1.7 using the following steps:
Downloaded the files from https://github.com/VMML/vmmlib/tree/1.7
and followed the steps below:
mkdir vmmlib/build
cd vmmlib/build
cmake ..
make
Now, I'm trying to run a program in the test folder "t3_hosvd_test.cpp" , but I'm not able to run it.
I tried to run the Makefile but got the following error:
svd_test.cpp:13:0:
../include/vmmlib/vmmlib.hpp:33:30: fatal error: vmmlib/version.hpp: No such file or directory
compilation terminated.
: recipe for target 'svd_test.o' failed
make: *** [svd_test.o] Error 1
I also tried to run by giving the following command in the terminal:
g++ t3_hosvd_test.cpp -lapack -lvmmlib
but got an error:
t3_hosvd_test.cpp:2:31: fatal error: vmmlib/t3_hosvd.hpp: No such file or directory
compilation terminated.
Can someone help me compile and run this program on Ubuntu 16.04?
When compiling directly, you need to provide the include path via the -I option.
g++ t3_hosvd_test.cpp -I/usr/local/vmmlib17/include -lapack -lvmmlib
Also ensure that your include path is set to vmmlib/include and not vmmlib/include/vmmlib

Is it possible to compile c++ using Clang only on windows?

I really tried a lot. Clang does not come with standard C++ includes, and obviously can not find them :
clang++ file.cpp -o file.out
C:\Folder\file.cpp:1:11: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Passing the mingw includes by arguments, it returns another error:
clang++ -target x86_64-w64-mingw32 C:\Folder\file.cpp -IC:\MinGW\mingw64\lib\gcc\x86_64-w64-mingw32\6.3.0\include\c++ -IC:\MinGW\mingw64\lib\gcc\x86_64-w64-mingw32\6.3.0\include\c++\x86_64-w64-mingw32 -IC:\MinGW\mingw64\x86_64-w64-mingw32\include -o C:\Folder\file.out -std=c++11
clang++.exe: error: unable to execute command: program not executable
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
When I use -c it does not use the linker and also not generate an executable file.
Edit : I'm running on windows
Run your commands from "x64 Native Tools Command Prompt for VS 2017", or something similar. It will setup some required env vars for you.

make: g++: error: CreateProcess: No such file or directory

I see that this issue has been posted many many times but none of solutions worked for me and my problem is a bit different.
The problem:
When a use a makefile and mingw32-make, I get the error:
g++: error: CreateProcess: No such file or directory
However, if I copy/paste the SAME command that the makefile tried to do and paste it in the SAME command prompt it works. This problem only occurs when I try to build with a makefile.
Here's more info:
g++ ./src/main.cpp ./src/application.cpp -I C:\Code\infographie\Labs\inc -L C:\Code\infographie\Labs\lib -I C:\Code\infographie\Labs\inc -g -Dmain=SDL_main -L C:\Code\infographie\Labs\lib -lmingw32 -lSDL2main -lSDL2 -o bin/debug/labs.exe
g++: error: CreateProcess: No such file or directory
makefile:23: recipe for target 'all' failed
mingw32-make: *** [all] Error 1
As I said, If I copy/paste the first line it will work.
What can I try?
UPDATE: I've installed cygwin, adjusted SDL libs and recompiled. Now I get this error when I try to use make. If I copy/paste the command, it works.
g++ ./src/main.cpp ./src/application.cpp -I C:\Code\infographie\Labs\inc -L C:\Code\infographie\Labs\lib -I C:\Code\infographie\Labs\inc -g -Dmain=SDL_main -L C:\Code\infographie\Labs\lib -lcygwin -lSDL2main -lSDL2 - mwindows -o bin/debug/labs.exe
make: g++: Command not found
make: *** [makefile:23: all] Error 127
FIXED IT, don't declare a variable named PATH...
Stop using MinGW.
MinGW has not been updated since 2013. It has been replaced by Cygwin and
MSYS2. The MinGW project doesn’t even offer a 64-bit compiler.
Cygwin can be installed in about 3 minutes, I would give it a try.

Compile c++ code with IDE, error at the second time

I'm a fresh c++ user in Windows 8 system. I have installed MinGW and setup the environment variables. I create the simplest hellow world code as below
#include<iostream>
#include<vector>
using namespace std;
int main(int argc, char **argv)
{
cout<<"hello world"<<endl;
return 0;
}
It works well at the first time, but when I do some small modification, for example delete "hello", and then build and run the project again. I found that the program will keep running without end. The command line keeps showing:
Current working directory: C:\Users\cr\Documents\project\helloworld\Debug
Running program: le_exec.exe ./helloworld
If I try to build it again, it shows
make.exe[1]: Entering directory 'C:/Users/cr/Documents/project/helloworld'
C:/Users/cr/MinGW/bin/g++.exe -c "C:/Users/cr/Documents/project/helloworld/main.cpp" -g -O0 -Wall -o ./Debug/main.cpp.o -I. -I.
C:/Users/cr/MinGW/bin/g++.exe -o ./Debug/helloworld #"helloworld.txt" -L.
c:/users/cr/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot open output file ./Debug/helloworld.exe: Permission denied
collect2.exe: error: ld returned 1 exit status
make.exe[1]: *** [Debug/helloworld] Error 1
helloworld.mk:78: recipe for target 'Debug/helloworld' failed
make.exe[1]: Leaving directory 'C:/Users/cr/Documents/project/helloworld'
make.exe: *** [All] Error 2
Makefile:4: recipe for target 'All' failed
====1 errors, 0 warnings====
I could not even delete the executable file "helloworld.exe" because it is using in 'System' until I restart the computer.
Update:
I can compile it in the command line:
C:/Users/cr/Documents/project/helloworld>g++ main.cpp -o test.exe
C:/Users/cr/Documents/project/helloworld>test.exe
It will then keep running until I close the command line window. If I open a new command line window and compile the file again. There is no problem.
Update2:
Problem also happens when I compile the cpp file the second time even if the file has not been edited.
C:/Users/cr/Documents/project/helloworld>g++ main.cpp -o test.exe
C:/Users/cr/Documents/project/helloworld>test.exe
hello world
C:/Users/cr/Documents/project/helloworld>g++ main.cpp -o test2.exe
C:/Users/cr/Documents/project/helloworld>test2.exe
It keeps running here.
Thanks all, I struggled with it for a whole day. The problem is anti-virus software...... I set it inactive and all things do well...
I don't want to point out what this software is, but just want to give out a reminder for this problem....

Can't build test application with WxWIdgets in Netbeans

I'm a total c++ Newbie.
I'm trying to build windows aplications with Wx-Widgets under Net Beans.
So far so good. I have instaled everything, configured, build wx-widgets and attached it to the new project - with help of a good man here: Can't make wx-widget work with net-beans.
Now I try to build my first app:
#include <wx/string.h>
int main(int argc, char **argv)
{
wxPuts(wxT("A wxWidgets console application"));
}
and this is what I get:
I'm not sure if this is readable so I paste the errors:
g++ `C:\WXWIN\wx-config --cxxflags` -c -g -I../../../WXWIN/include `C:\WXWIN\wx-config --cxxflags` -MMD -MP -MF build/Debug/MinGW-Windows/main.o.d -o build/Debug/MinGW-Windows/main.o main.cpp
/bin/sh.exe: C:WXWINwx-config: command not found
/bin/sh.exe: C:WXWINwx-config: command not found
In file included from ../../../WXWIN/include/wx/defs.h:21:0,
from ../../../WXWIN/include/wx/string.h:25,
from main.cpp:1:
../../../WXWIN/include/wx/platform.h:196:22: fatal error: wx/setup.h: No such file or directory
compilation terminated.
make[2]: *** [build/Debug/MinGW-Windows/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
make[2]: Leaving directory `/c/xampp/htdocs/WxWwigets_tutorial'
make[1]: Leaving directory `/c/xampp/htdocs/WxWwigets_tutorial'
BUILD FAILED (exit value 2, total time: 859ms)
Please notice that I have set everything in linker and compiler like ordered: http://wiki.wxwidgets.org/Compiling_using_Netbeans
The error is right in front of you: "command not found". If you use /bin/sh, you must use Unix style paths, e.g. C:/WXWIN/wx-config instead of DOS paths with the backslashes.
I seriously advise you to get familiar with the environment you're using instead of just following the Wiki instructions without understanding them, otherwise your problems won't be over any time soon.