Qter 发表于 2022-4-25 22:56:36

Makefile和跨平台开发

https://www.it1352.com/1592429.html

Makefile和跨平台开发[英] Makefiles and cross platform development查看:292 发布时间:2020/5/5 12:11:50 c cross-platform makefile

本文介绍了Makefile和跨平台开发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述我一直试图弄清楚如何创建一个可以为所有主要操作系统编译的C程序.我已经考虑过使用makefiles,所以我只需要更改目标操作系统,但是有一些问题.我的第一个问题是我无法弄清楚如何更改目标操作系统,因此可以在一个操作系统上进行编译,但可以在所有操作系统上使用该应用程序.我的第二个问题是我无法弄清楚如何使其自动编译src目录中的所有.c文件,因此不必在每次添加新文件时都修改makefile.有人知道该怎么做吗?I have been trying to figure out how to create a C program that can be compiled for all of the major operating systems. I have considered using makefiles so I would just have to change the target OS, but I have a few problems. My first problem is that I cannot figure out how to change the target OS so I can compile on one OS but use the application on all OS's. My second issue is that I cannot figure out how to make it automatically compile all .c files in the src directory, so I do not have to modify the makefile every time I add a new file. Does anybody know how to do this stuff?我当前的Makefile(目前未从此处修改)My current makefile (currently unmodified from here)
CC=gcc
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=main.cpp hello.cpp factorial.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=hello

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
    $(CC) $(CFLAGS) [        DISCUZ_CODE_0        ]lt; -o $@q2
推荐答案GNU Autoconf是一个不错的起点.我无法为您提供完整的答案,因为除了维护一组nmake脚本外,我不知道您对Windows会做什么.尽管存在学习曲线,但它们使得编写跨平台工作的make文件和c代码成为可能.它不仅可以为您处理多个源文件,而且可以帮助您定义目标,例如全部",安装",干净"等.如果您熟悉下载项目,然后键入"./configure",然后如果执行"make install",则您可能是Autoconf生成的项目的用户. The GNU Autoconf is a good place to start. I can't give you a complete answer because I don't know what you'd do for Windows except maintain a set of nmake scripts. While there's a learning curve they make it possible to write make files and c-code that work across platforms. It will not only handle multiple source files for you but help you define target such as 'all', 'install,' 'clean,' etc. If you're familiar with downloading a project and then typing './configure' and then 'make install' then you've probably been a user of an Autoconf generated project.使用Autoconf的其他原因不仅仅是处理多个源文件.There are other reasons to use Autoconf than just handling multiple source files.难以在Unix风格之间进行C/C ++开发的是"Unix"和C的定义.不同Unix风格的库或系统调用略有不同,具体取决于它们所支持的标准风格. C也是如此.因此,在编写代码时,您将看到#defines指定一组预处理器符号,这些符号定义了您要使用的Unix和C版本.从GCC到HP C或其他供应商的C编译器可能会包含不同的默认"行为级别,因此您需要明确预期的行为.What makes C/C++ development across flavors of Unix difficult is the definition of 'Unix' and C. Different flavors of Unix have slightly different library or system calls depending on what flavor of standards they support. The same is true of C. So, when writing code you'll see #defines specifying a group of preprocessor symbols that define a version of Unix and C that you'd like to use. Going from GCC to HP C or some other vendor's C compiler may include different 'default' levels of behavior, so you need to make the expected behavior explicit.第二,您需要定义所需的库.在您的构建中,您可能需要探查mysql库并确定如果未安装这些库该怎么办.在某些情况下,您可能根本不需要构建,或者在某些情况下,您可能只是替换了另一个库(如libdb).尝试编写复杂的makefile文件以支持甚至两个操作系统(Solaris和Linux)后,我也不想重新发明这个轮子.Second, you need to define what libraries are required. In your build you might need to probe for mysql libraries and decide what to do if those libraries are not installed. In some cases you might need to not build at all or in some cases you might just substitute for another library (like libdb). Having tried writing complex makefiles to support even two operating systems (Solaris and Linux), I would not want to re-invent this wheel.



页: [1]
查看完整版本: Makefile和跨平台开发