简体   繁体   中英

command line arguments to execute a C++ program

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.

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.

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

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).

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM