2

this is the main of my c++ program:

void main(int argc, char** argv, Arguments& arguments)

the first arguments is a file and the rest are boolean values.
I was wondering what is the correct syntax for the command line to compile the program.
I tried:

gcc  -o "argument1" "argument2" "argument3" prog.cpp  

and

g++ -std=c++11 -o "argument1" "argument2" "argument3" prog.cpp

but I get this error:

linker command failed with exit code 1 (use -v to see invocation)

I am doubting that I am not passing the arguments correctly and therefore my program doesn't link to the input file (argument1) correctly.
thank you for correcting me.

  • 9
    That's not a valid prototype for `main`. – molbdnilo Apr 25 '18 at 11:59
  • 3
    You do not use `gcc` to run a C++ program. You compile a C++ program to an executable. Then you run the executable, for example from your shell. – Micha Wiedenmann Apr 25 '18 at 12:00
  • 1
    It probably is according to academia. – Ron Apr 25 '18 at 12:00
  • You should also use `g++` to compile C++. (Or pass `gcc` the correct parameters, which is too much hassle for anyone to do, so use `g++`.) – molbdnilo Apr 25 '18 at 12:03
  • 3
    There is a list of good books [here](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo Apr 25 '18 at 12:04
  • I get the same issue when I use: 'g++ -std=c++11 -o "argument1" "argument2" "argument3" prog.cpp ' and I don't get an executable. –  Apr 25 '18 at 12:06
  • @waffles123 Because that's not a valid signature of `main`? As was stated in the first comment? – Algirdas Preidžius Apr 25 '18 at 12:07
  • What instructions are you following? – Galik Apr 25 '18 at 12:13
  • Possible duplicate of [What is the proper declaration of main?](https://stackoverflow.com/q/4207134/608639), [What should main() return in C and C++?](https://stackoverflow.com/q/204476/608639), and [How to pass command line arguments to a c program](https://stackoverflow.com/q/17645447/608639) – jww Apr 25 '18 at 23:56

4 Answers4

13

Main and Command Line Arguments

Main can have one of two forms:

int main()
int main(int argc, char** argv)

In the first form, you cannot pass any arguments. In the second form argc is a count of the arguments passed on the command line, and argv is an array of char* (c-style strings) of length argc containing the command line arguments.

So, for example, if you called your program as

./program apple bananna carrot date

Then argc would be equal to 5 and argv would contain the following values:

argv[0] = "./program" -- the name of your program as called on the command line. 
argv[1] = "apple"
argv[2] = "bananna"
argv[3] = "carrot"
argv[4] = "date"

Compiling and Running your Program

C++ is not an interpreted language and must therefore be compiled. Assuming you have your source code in a file called program.cpp, and you want your executable to be called program, then you would invoke g++ as follows:

g++ -o program program.cpp

If you ls the current directory, you should now see a file called program in the directory beside your source code. You can now run this program (again, assuming you named the output file program)

./program arg1 arg2 arg3

and the strings arg1, arg2, and arg3 will be passed into main as described above.

Paul Belanger
  • 2,354
  • 14
  • 23
  • 1
    Note that a third form is often allowed by implementations, `int main (int argc, char *argv[], char *envp[]);` – Khoyo Apr 25 '18 at 13:23
7

A main function is defined like this:

int main (int argc, char *argv[])

or

int main (int argc, char **argv)

As I understand it, argc = Argument Count and argv = Argument Vector. argc is the number of arguments (you can choose how many), and argv contains that number of arguments, which contain all the actual data you want to pass in to your program from the command line. But remember there is always at least one argument which comes first: The name of the program.

These are not used during compilation but during run time. Running the program is different from compiling and linking, which have to be done first (using gcc, in your case).

matt_rule
  • 1,220
  • 1
  • 12
  • 23
2

It seems that you are new because you have mingled a lot!

First you have to compile your program. For a cpp program usually g++ is used. So compile it with

g++ -Wall -o prg prg.cpp

Afterward you have to modify your access with

chmod +x prg

to be able to invoke the program.

Now you can call your program with your arguments:

./prg arg1 arg2 arg3
abu_bua
  • 1,361
  • 17
  • 25
  • 3
    On most systems, an executable file created by g++ will automatically be flagged executable, so the `chmod` would not be necessary. (If not, you may have a strange `umask` setting.) – aschepler Apr 25 '18 at 12:21
1

C++ coding/program execution process is as follows (at least for simple one file programs):

Step 1: Write the code, say in a file called prog.cpp

Step 2: Compile the code into an executable. In our case, g++ -o myprog prog.cpp

Step 3: Execute the program. In our case, myprog "argument1" "argument2" "argument3"

mahesh
  • 1,028
  • 10
  • 24