Running c++ file with raspberry pi - c++

I was making a simple hello world c++ program. For some reason, it won't run after I compile it. Here's the program:
#include <iostream>
using namespace std;
int main() {
cout << "hello world";
}
I compiled using:
g++ -std=c++0x helloworld.cpp
No errors. However, when I tried running it using ./helloworld.cpp, I got this:
./helloworld.cpp: line 2: using: command not found
./helloworld.cpp: line 5: syntax error near unexpected token `('
./helloworld.cpp: line 5: `int main()'
Of course, I tried looking this up, and found a link that had someone asking almost the exact same question as mine. (C++ compiles but gives error when executed). They told me to remove the .cpp. However, I tried doing ./helloworld and I still got errors. It told me this:
bash: helloworld: No such file or directory
Also, I was in the directory with helloworld.cpp in it, so I don't think that was the problem. Any help would be appreciated. Thanks!

g++ -std=c++0x helloworld.cpp
should have left you with an a.out file that you can execute.
However, when I tried running it using ./helloworld.cpp, I got this:
...
You can't execute the helloworld.cpp source from the shell.
You probably should use
g++ -std=c++0x helloworld.cpp -o helloworld
# ^^^^^^^^^^^^^
to name the executable file other than a.out
You can call ./helloworld then to run your compiled program.

You can't execute a .cpp file. Find where the compiled program is and run that.

The .cpp file is the file you wrote. It's a text file, so you obviously can't "run" it. If you build a program you create an executable, which you can execute. This is a different file.

Related

Having difficulties compiling simple c++ program on the command line with multiple files; maybe a linker error?

Sorry for the simple question. I am attempting to learn more c++ at a fundamental level. I have always used VS in the past, and I am trying to learn the command line and compile, navigate, etc. with it.
I started with "hello world" and was able to compile it with gcc/clang, then run it with the expected results.
I then slightly reworked this and made a new header/cpp file to do the output part of hello world, and then call that from the main function, described below:
main.cpp:
#include "MyClass.h"
int main(){
foo();
return 0;
}
MyClass.h
#pragma once
void foo();
MyClass.cpp
#include "MyClass.h"
#include <iostream>
void foo(){
std::cout << "Hello World\n";
}
I then have tried to compile with gcc and clang as follows:
clang -Wall -g main.cpp MyClass.cpp
I have tried the same with GCC, and have also tried various invocations of this, such as using -c:
clang -Wall -g -c main.cpp
clang -Wall -g -c MyClass.cpp
Each and every time, I get an error
λ clang -Wall -g MyClass.cpp main.cpp
main.cpp:13:1: error: use of undeclared identifier 'foo'
foo();
^
1 error generated.
I get this same error whether using gcc or clang.
I also tried from scratch on my laptop, to see if there was some more global issue, but I still get the same problem.
I have also tried on the basic Windows command line as well.
Other areas on StackOverflow demonstrate simple ways of compiling multiple files from the command line, and I have tried as they show, but still get errors.
I also know that "make" is something I need to learn as well, however, I just want to make sure I understand what my make file is doing before I dive into that.
I feel like it must be something trivial that I just cannot figure out.
Thank you to Andreas for the suggestion of looking at the preprocessor output. And thank you to everyone for the suggestions.
The pre-processor output did not make sense to what I was compiling.
I was using VSCode, in this case, as a text editor, making brand new files in my folder after launching it from the command line. I thought the files I created in VSCode directly into the folder (named main.cpp, for example), would produce a regular text file. However, for some reason, it did not.
Essentially, I recreated the above program in notepad and was easily able to compile it using the commands I used above. I guess VSCode may not be perfect for me as a pure text editor or I should figure out if there are settings to change to accomplish my goal.
Thank you all again for your time and consideration.
Use extern on your function. Also make sure you're compiling with c++ and not c; i.e. g++.
MyClass.h
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
extern void foo();
#ifdef __cplusplus
}
#endif

Visual Code C++ "No such file or directory" message when trying to compile

I just started to get into coding. I eventually got JavaScript code to start working in Visual Studio Code, but I am having significant trouble with C++.
The error message:
My tasks.json file:
My launch.json file:
https://imgur.com/a/LtKufVq
Could any explain to me, someone who's relatively new to this, how I can get this to properly work?
The file name for your c++ source appears to be "test.cpp," but your compilation command line is looking for the file, "Testing.cpp":
g++ -g Testing.cpp
Try changing this to:
g++ -g test.cpp

MinGW gcc gives errors when other compilers do not

Before you asked, yes i did look this up FOR DAYS. Im completely stuck... I'm using MINGW32 (my shortcut says MSYS) to compile my c and cpp code. For about 2 or 3 days now I have been getting strange errors. (below) It was working JUST FINE before. I even ran the same code i've compiled before and it gave the same error. I then go into DevC++ and open then compile an it works just fine.
ERRORS:
namespace: command not found
using: command not found
syntax error:
int main(){
(sometimes it gives me a big unreadable mess)
I'm really stuck... I dont want to have to switch to DevC++... I like to use my own text editor and compile in a command line.
From your errors namespace: command not found and using: command not found says to me that you aren't compiling the code with an appropriate compiler.
For reference in MinGW32 toolchain:
gcc.exe = C
g++.exe = C++
You may find it useful to take a look at what the IDE's actualy do with your compiler.
My current IDE allows me to see all the commands that it runs to build my project:
C:/mingw32/bin/g++.exe -c "C:/MyProgram/main.cpp" -g -O0 -std=c++14 -Wall -o ./Obj/main.cpp.o -I. -IDependencies/Something/include
So lets examine what this does.
My current toolchain is MinGW32 which is located in C:/mingw32/bin/g++.exe
g++ is our c++ compiler so we call g++.exe and we pass the following switch:
-c "C:/MyProgramm/main.cpp"
This tells my compiler to compile the main.cpp from my project directory. then my IDE adds a few additional command line switches. For the purpose of the answer I will only consider -o. This tells us the output file from our code main.cpp into an output file.
The reason we produce such a file is to save us time compiling so that we do not have to compile the same file twice without making changes to it. We perform this step on each of our files creating a collection of .o files.
The new file is then saved in "C:/MyProgramm/Obj/main.cpp.o"
Which means that your command line function will look something like this:
C:/mingw32/bin/g++.exe -c "<my project directory>/<file>.cpp" -o ./Obj/<file>.cpp.o
I would recommend that you read up on documentation for the g++ function and learn from different IDE's as you will soon find that you need to do more advanced things with your compiler.
For example to enable features from c++14 I add -std=c++14
*Edited to reflect feedback.

Errors thrown in C++ basic program

I'm a bit of a newbie to C++, but I have some programming experience. I made a basic program, following a guide I found on t'internet. It compiled with g++ easily, but when I ran it it threw these errors:
./FP.cpp: line 1: //: Is a directory
./FP.cpp: line 3: using: command not found
./FP.cpp: line 5: syntax error near unexpected token ('
./FP.cpp: line 5:int main ()'
I'm using Geany on a Raspberry Pi (but using a command line to run the program as the Geany interpreter doesn't work). Here's the program:
// First program in c++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
Any ideas?
Cheers!
It looks like you're trying to execute the source file, i.e. run it as a shell script.
To compile your program you would do something like this:
$ g++ -Wall FP.cpp -o FP
This produces an executable named FP. So you should now see both the original program, FP.cpp, and the executable, FP, in your current directory.
To run the executable (compiled program):
$ ./FP
Calling ./FP.cpp tries to execute the source code, which is not what you want. You need to compile it via g++, and then run the executable (usually ./FP)

minGW CPP G++ Proper Command to Compile

I installed the following:
MINGW32_NT-6.1 i686 Msys
I am working with the command line.
Wrote the "typical" HelloWorld.cpp program.
IF I compile with: cpp HelloWorld.cpp -o HelloWorld.exe COMPILE is good. (18k)
BUT execution fails: 16 bit MS-DOS Subsystem. NTVDM CPU error
IF I compile with: g++ HelloWorld.cpp -o HelloWorld.exe COMPILE is good. (48k)
Execution is good.
I cannot determine the BEST way to execute the compile and what the difference is between the methods. Any suggestions? or good references?
THANKS.
"cpp" is the "C PreProcessor", not the compiler. So you're just getting something strange in HelloWorld.exe
Execute the "type HelloWorld.exe" and see what it gives. It shouldn't even be a binary file - just a long text file with all the "#includes" and "#defines" replaced.
To your question - the second way is "right", because you actually invoke the compiler/linker and produce a valid executable. The first "way" is a valid command, but it has almost nothing to do with compilation and linking.