Need help with running a program.

Oct 31, 2018 at 4:52am
I have to run a program that has a total of three files.

1
2
3
4
5
6
//A header file.
Student.h
//A cpp file.
Student.cpp
//A main file.
main.cpp


My professor did mention to use #pragma once in the header file, but how would you run the program through the terminal with these three files?
Last edited on Nov 18, 2018 at 9:17pm
Oct 31, 2018 at 9:45am
You need to compile them into an executable…
Is this the first time you deal with a program split into more than one file?
Don't you use an IDE (Visual Studio, Code::Blocks…)?
Nov 1, 2018 at 1:35am
It is actually,
I use virtual box.
Nov 1, 2018 at 2:20am
Assuming you're compiling from the terminal with g++:


$  g++ -std=c++11 -Wall -Wextra -c Student.cpp
$  g++ -std=c++11 -Wall -Wextra -c main.cpp
$  g++ -std=c++11 -Wall -Wextra -o myprogram Student.o main.o

That's the general process. Each cpp file compiles to an object (.o) file and those are then "linked" together into the executable (the last command). But you can actually compile it in one command like this:


$  g++ -std=c++11 -Wall -Wextra -o myprogram main.cpp Student.cpp


The first way allows you to compile separate cpp files which is useful when you have a bunch of them, but the process is usually controlled by a makefile (or cmake or an ide, etc.).
Last edited on Nov 1, 2018 at 2:23am
Nov 1, 2018 at 3:17pm
Topic archived. No new replies allowed.