0

I am new to C and using GCC. How do I compile multiple C files and then run them? I have multiple miles and each has different functions and they are supposed to run through the main.c file. My friend showed me through Windows but I am having issues figuring out how to do it on Mac.

What I was told: Compile both files individually first:

gcc -Wall -c .\main.c
gcc -Wall -c .\file.c

Then compile both together into an executable:

gcc -o program file.o main.o

Then run executable with .\program.exe

2
  • gcc -Wall -Wextra -Werror -pedantic -o program ./main.c ./file.c will build what you want in a single command. Beyond that, consider using a makefile or cmake (strongly advise the latter, btw). Commented Mar 8, 2022 at 3:23
  • The problem is the executable is not called program.exe on the mac, and the mac doesn't use backslashes as path separators. So the correct command is ./program not ./program.exe Commented Mar 8, 2022 at 3:25

1 Answer 1

2

You should probably investigate makefiles, but this is quite easy. The following should do the trick.

gcc -o program file.c main.c

Feel free to add in whichever -W warning flags you want.

Note also that Macs do not use \ as a directory separator but rather /, and executable files do not typically end in .exe.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm still having issues compiling. I did: 'gcc main.c mealExpenses.c miscExpenses.c travelTime.c vehicleExpenses.c -o Program' and 'gcc -o program main.c mealExpenses.c miscExpenses.c travelTime.c vehicleExpenses.c' but no luck. Any suggestions?
Try having main.c be the last file listed. The order does matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.