Running .cpp files in the command line

Hello, my intro C++ course is requiring us to run our .cpp files in the command line. I downloaded and set up paths for mingw files. However, every time I try to compile and run the .cpp file, I get these bizarre errors.

What I do:
C:\Users\name\Desktop>\mingw\bin\gcc HelloWorld.cpp
*then it pauses as it attempts to compile, then it prints*

1
2
3
4
5
6
7
8
C:\Users\namel\Desktop>\mingw\bin\gcc HW.cpp
C:\Users\name\AppData\Local\Temp\ccnuvZeB.o:HW.cpp:(.text+0x21): undefined reference to `std::cout'
C:\Users\name\AppData\Local\Temp\ccnuvZeB.o:HW.cpp:(.text+0x26): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
C:\Users\name\AppData\Local\Temp\ccnuvZeB.o:HW.cpp:(.text+0x2d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
C:\Users\name\AppData\Local\Temp\ccnuvZeB.o:HW.cpp:(.text+0x34): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
C:\Users\name\AppData\Local\Temp\ccnuvZeB.o:HW.cpp:(.text+0x54): undefined reference to `std::ios_base::Init::~Init()'
C:\Users\name\AppData\Local\Temp\ccnuvZeB.o:HW.cpp:(.text+0x75): undefined reference to `std::ios_base::Init::Init()'
collect2.exe: error: ld returned 1 exit status


I'm noticing a lot of C++11 references, so perhaps something is outdated regarding my code? I searched around and one forum suggested the error is because "gcc" can't find C++ libraries, and that I should use "g++" instead.

So I type: C:\Users\name\Desktop>\mingw\bin\g++ HelloWorld.cpp

Yet this returns nothing. The cmd pauses, and then returns back to:

C:\Users\name\Desktop>

I would really appreciate it if someone could explain perhaps what I'm doing wrong with my files.

Note: I am positive my code compiles correctly in the IDE
Last edited on
>\mingw\bin\g++ -std=c++11 -O2 -Wall -Wextra -pedantic-errors HelloWorld.cpp && a.exe

The first part (before the &&) compiles and links the C++ source code to produce the executable a.exe
The second part (after the &&) runs the program (if the first part completed without errors)
At risk of repeating JLB (it *is* my name!),

first you compile the code to create the program, and then you run the program you just created.

You were only doing the first thing.
Thank you, JLBorges so very much! Surely there's a simpler way to do this. Is c++11 the current standard? I've seen people simply type the cpp filename with the g++ or gcc and have the program execute
Not sure I understand, Repeater.

If I type the cpp file in the command bash that means I'm simply compiling? Even if its throwing those error statements? What is the command to run the program after that drop down of statements?
All those errors are the reason it didn't compile. If it didn't compile, then you have no program to run.

I've seen people simply type the cpp filename with the g++ or gcc and have the program execute

I expect you've seen people create programs with the same name as the cpp source file.

For example, you could have a cpp source file called "program.cpp" and you could then compile it into a program called "program.exe" ; two different files, with similar names.

When you type this:
\mingw\bin\g++ HelloWorld.cpp
you are running the program named "g++" and you're asking it to create a new program, using HelloWorld.cpp as the source file. This is how the command line works. You pick a program to run, and sometimes you feed it some extra input; in this case, the program to run is g++, and the extra parameter that you are giving to g++ is the string "HelloWorld.cpp".

You can think of gcc as the program that turns C code into programs. That's why you had the problem; you were running a program that turns C code into programs, but you're got C++ code. You could think of g++ as the program that turns C++ code into programs.

You didn't tell g++ what to name the new program, so it will have picked something for you. So take a look in the directory that you were in when you ran g++, and see if there's a new exe file in there ; that will be the program you created.

Is c++11 the current standard?

C++17 is, but this has nothing to do with C++ standards. This is about how compiled programming languages work. Your "intro to C++" course has unfortunately skipped a lot of the basics of how programming works and how programs get made.

You could read everything you missed here:
https://www.daniweb.com/programming/software-development/tutorials/466177/understanding-c-from-source-to-binaries

Last edited on
Ah, I understand. Indeed, I've noticed the cpp and .exe duplicates with my files. I understand and will read up on it, thank you!
If I may ask one more thing:
So now I understand the brief pause when inserting myfile.cpp is the compiling, and if it returns nothing that means there were no compiler errors. Then what would be the next time to actually run the program from there? Would it be \ming\bin\g++ HelloWorld.exe ?
Oh nevermind I figured it out.

Use: mingw\bin\g++ HelloWorld.cpp && HelloWorld.exe
*then it runs*

Great, appreciate the help.
mingw\bin\g++ HelloWorld.cpp && HelloWorld.exe
means run the command mingw\bin\g++ HelloWorld.cpp , and then afterwards run the command HelloWorld.exe.

To run your program, you type its name: Helloworld.exe

If you're going to be using computers, definitely time to learn how to use them; how to run programs, that sort of thing.
means run the command A , and then afterwards run the command B

Almost, but not quite.

A && B
definitely does run command A, but it will run B only if A returns "success".

The && is lazily evaluated logical AND, just like in C++.

On command line, unlike in C++, you can chain multiple commands:
A && B && C || D

(At least on some shells.)
Topic archived. No new replies allowed.