Makefile 编译c/h?
我需要编译一个旧的应用程序,其 tarball 仅包含 *.c 和 *h,即。没有生成文件。根目录包含应用程序,子目录包含应用程序所需的库。
我的 make/Makefile 知识不是很好,我想知道编译这个应用程序+库的最简单方法是什么。
谢谢。
编辑:使用这个脚本...
# cat compile.bash
#!/bin/bash
cd mylib
for cfile in *.c; do
ofile=$(echo "$cfile" | sed 's#.c$#.so#')
gcc -shared -c "$cfile" -o "$ofile"
done
cd ..
gcc *.c -I mylib -L mylib -mylib -o myapp
...我注意到 mylib/ 中的每个 *.c 文件都被编译成 *.so 文件,而不是将每个文件编译成目标文件并构建单个 .so 文件,我得到了很多警告和错误,例如。
unzip.c: In function âunzipâ:
unzip.c:991: warning: format not a string literal and no format arguments
gcc: unrecognized option '-mylib'
file_util.c: In function âfile_moveâ:
file_util.c:98: error: âerrnoâ undeclared (first use in this function)
我不知道如何编译库,然后编译应用程序而不会出现错误/警告。
I need to compile an old application whose tarball only contains *.c and *h, ie. no Makefile. The root directory contains the application, and a sub-directory contains a library the application needs.
My make/Makefile knowledge isn't great, and I was wondering what the easiest way would be to compile this application + library.
Thank you.
Edit: Using this script...
# cat compile.bash
#!/bin/bash
cd mylib
for cfile in *.c; do
ofile=$(echo "$cfile" | sed 's#.c$#.so#')
gcc -shared -c "$cfile" -o "$ofile"
done
cd ..
gcc *.c -I mylib -L mylib -mylib -o myapp
... I notice that each *.c file in mylib/ is compiled into a *.so file instead of compiling each into an object file and building a single .so file, and I get tons of warnings and errors, eg.
unzip.c: In function âunzipâ:
unzip.c:991: warning: format not a string literal and no format arguments
gcc: unrecognized option '-mylib'
file_util.c: In function âfile_moveâ:
file_util.c:98: error: âerrnoâ undeclared (first use in this function)
I don't know how to compile the library, and then compile the application without error/warning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无需使用 for 循环或生成中间目标文件:
No need to use a for loop or generate intermediate object files:
编译库:
之后,libfoo/ 中应该有一个
libfoo.so
文件。然后,编译程序(不要忘记cd
返回):Compile the library:
After this, you should have a
libfoo.so
file in libfoo/. Then, compile the program (Don't forget tocd
back):最简单的方法可能是使用 IDE 来为您进行构建。 Netbeans 会创建一个 Makefile,这样您就可以独立于 IDE 构建项目。
The easiest is probably to get an IDE to do the build for you. Netbeans for one will create a Makefile so you can then build the project independently of the IDE.