I’m compiling programs via command-line with mingw32 gcc. As the source files increase, I’m running into some problems. Firstly, consider the following script:
gcc -Wall -I"MyDir" -I"MyDir2" -L"MyLibDir" -L"MyLibDir2" -l lib1 -l lib2 -l lib3 -c file.c
gcc -Wall -I"MyDir" -I"MyDir2" -L"MyLibDir" -L"MyLibDir2" -l lib1 -l lib2 -l lib3 -c file2.c
gcc -Wall -I"MyDir" -I"MyDir2" -L"MyLibDir" -L"MyLibDir2" -l lib1 -l lib2 -l lib3 -c file3.c
gcc -o myprog.exe file1.o file2.o file3.o
Now, can’t I just tell gcc the compiler directories -I, linker directories -L and link libraries -l only once and then compile the second and third source file with the same options without having to retype them? Something like
gcc -define-options -Wall -I"MyDir" -I"MyDir2" -L"MyLibDir" -L"MyLibDir2" -l lib1 -l lib2 -l lib3
gcc -Wall -c file1.c
gcc -Wall -c file2.c
gcc -Wall -c file3.c
gcc -o myprog.exe file1.o file2.o file3.o
As far as I know, no, you can’t. But that’s why make and Makefiles were invented 🙂 I think this tutorial is more than enough to get you started on writing a decent Makefile for your small project.
PS: The quick & dirty way would be to just write a simple bash script that holds the include directories in a variable, but Makefiles are a much better option in my opinion, since you don’t want to reinvent the wheel.
PS2: Out of curiosity, I found that, indeed, there is a way to do it. You just need to define some environment variables. See here for details: How to add a default include path for gcc in linux?