3

I've written a program in C, and I'm trying to compile it on command-line with arguments, but when I write an integer, for example:

gcc findtopk.c -o findtopk 500 5 test1.txt test2.txt test3.txt test4.txt test5.txt outfile.txt

it says:

gcc: error: 500: No such file or directory
gcc: error: 5: No such file or directory
1
  • 5
    Are you trying to pass run time arguments to your program at compile time? that's not going to work AFAIK - the only non-option arguments accepted by gcc are the names of files to compile Oct 27 '19 at 15:33
11

You appear to be trying to pass run time arguments to your program at compile time - as far as I know, that doesn't work. The only non-option arguments accepted by gcc are the names of files to be compiled.

It's hard to give precise instructions because you haven't shared the source code of your program (or explained how it handles arguments) but probably what you want is something like:

gcc findtopk.c -o findtopk

to compile and link your program; then

./findtopk 500 5 test1.txt test2.txt test3.txt test4.txt test5.txt outfile.txt

to run it with argument list test1.txt test2.txt test3.txt test4.txt test5.txt outfile.txt.

The details of argument parsing in C are off-topic here but there are plenty of online resources, for example

2
  • It's actually not unheard of for compilers to have a mode where they compile and immediately run the source file (much like one would run an interpreter). For example, C# and Java ended up having such modes, and I'm sure I've seen some C compiler support it (although it seems it was neither gcc nor clang).
    – user1686
    Oct 28 '19 at 9:44
  • @grawity Probably tcc (stackoverflow.com/questions/2482348/run-c-or-c-file-as-a-script)
    – jpa
    Oct 28 '19 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.