简体   繁体   English

使用MinGW32 GCC进行编译。 只告诉链接库一次?

[英]Compiling with MinGW32 GCC. Only tell link libraries once?

I'm compiling programs via command-line with mingw32 gcc. 我正在通过命令行使用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? 现在,我不能告诉gcc编译器目录-I ,链接器目录-L和链接库-l只有一次,然后使用相同的选项编译第二个和第三个源文件而不必重新键入它们? 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. 但这就是make和Makefiles被发明的原因:)我认为这个教程足以让你开始为你的小项目编写一个像样的Makefile。

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. PS:快速而肮脏的方法是编写一个简单的bash脚本,将include目录保存在变量中,但在我看来Makefiles是一个更好的选择,因为你不想重新发明轮子。

PS2: Out of curiosity, I found that, indeed, there is a way to do it. PS2:出于好奇,我发现确实有办法做到这一点。 You just need to define some environment variables. 您只需要定义一些环境变量。 See here for details: How to add a default include path for gcc in linux? 有关详细信息,请参阅此处: 如何在linux中为gcc添加默认包含路径?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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