0

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
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153

1 Answers1

2

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?

Community
  • 1
  • 1
Mihai Todor
  • 8,014
  • 9
  • 49
  • 86
  • 1
    A nit with the linked tutorial: calling a target `all` does nothing special. `make` considers the first target read while `.DEFAULT_GOAL` is empty to be the default, unless an assignment to the special variable `.DEFAULT_GOAL` changes this. See http://www.gnu.org/software/make/manual/html_node/Special-Variables.html . – Jack Kelly Jul 25 '12 at 09:30