用于在不同平台上编译的编译器指令

发布于 2024-08-10 23:55:09 字数 586 浏览 2 评论 0原文

我正在编译一个演示项目。

该项目是为 Windows 和 Linux 编写的。我已经写了一个Makefile。但是,我不确定如何指定编译器将在其上编译的平台。

我将在 Linux 上进行编译。

在我的源文件中,我有这个:

#if defined(WIN32)
#include ...
#include ...
#elif defined(LINUX)
#include ...
#include ..
#else
#error "OS not supported"
#endif

我的简单 Makefile 是这样的。当我编译时,出现错误“操作系统不支持”。

如何添加该指令,以便它可以使用 #elif 定义的(LINUX)进行编译。

LIBS_PATH = -L/usr/norton/lib
INC_PATH = -I/usr/norton/include

LIBS = -lntr

app: *.cpp *.h Makefile

    g++ $(LIBS_PATH) $(INC_PATH) *.cpp -o app

非常感谢您的任何建议,

I am compiling a demo project.

The project is written for windows and linux. I have written a Makefile. However, I am not sure how to specify the platform the compiler will be compiling on.

I will be compiling on Linux.

In my source file I have this:

#if defined(WIN32)
#include ...
#include ...
#elif defined(LINUX)
#include ...
#include ..
#else
#error "OS not supported"
#endif

My simple Makefile is this. And when I compile I get the error "OS not supported".

How can I add the directive so that it will compile with the #elif defined(LINUX).

LIBS_PATH = -L/usr/norton/lib
INC_PATH = -I/usr/norton/include

LIBS = -lntr

app: *.cpp *.h Makefile

    g++ $(LIBS_PATH) $(INC_PATH) *.cpp -o app

Many thanks for any suggestions,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你是年少的欢喜 2024-08-17 23:55:09

决定哪个将成为您的默认平台 - 比如说 LINUX。

LIBS_PATH = -L/usr/norton/lib
INC_PATH  = -I/usr/norton/include
LIBS      = -lntr
PLATFORM  = -DLINUX
CXX       = g++

app: *.cpp *.h Makefile
    ${CXX} ${CFLAGS} ${PLATFORM} ${INC_PATH} *.cpp -o $@ ${LIBS_PATH} ${LIBS}

您可以使用圆括号代替大括号。这使用了 C++ 编译器的宏,允许您通过 CFLAGS 添加其他标志(尽管通常也由“make”设置),并将平台、包含路径、库路径和实际库添加到编译行。

请注意,每次发生任何更改时,您的规则都会强制对所有内容进行完整的重新编译。这是“安全”的,但不一定有效。请注意,通配符也很危险 - 对于源来说比标头更危险。您可以在构建中包含文件的备份副本(old-reader.cpp - 您实际上只需要 reader.cpp)。更传统的做法是,列出程序所需的目标文件,以便在需要时可以单独重建每个目标文件,并将结果链接在一起。如果你的依赖关系正确(一个适度大的“如果”),那么就没有问题。如果你弄错了,你可能会得到不一致的程序。

但是,如果 5 秒重新编译和 5 分钟重新编译之间存在差异,您可能应该进行 5 分钟重新编译(如图所示)并在等待时回答另一个 SO 问题。

在 Linux(64 位)上编译:

make CFLAGS="-m64"

在 Linux(32 位)上编译:

make CFLAGS="-m32"

在 Windows 64 上编译:

make PLATFORM=-DWIN64

在 Windows 32 上编译:

make PLATFORM=-DWIN32

等等。

Decide which is going to be your default platform - say LINUX.

LIBS_PATH = -L/usr/norton/lib
INC_PATH  = -I/usr/norton/include
LIBS      = -lntr
PLATFORM  = -DLINUX
CXX       = g++

app: *.cpp *.h Makefile
    ${CXX} ${CFLAGS} ${PLATFORM} ${INC_PATH} *.cpp -o $@ ${LIBS_PATH} ${LIBS}

You can use round brackets in place of braces. This uses a macro for the C++ compiler, allows you to add other flags via CFLAGS (though that is also usually set by 'make'), and adds a platform, the include path, the library path and the actual library to the compile line.

Note that your rule enforces a complete recompilation of everything every time anything changes. This is 'safe' but not necessarily efficient. Note that wild-cards are dangerous too - more so for the source than the headers. You may include that backup copy of a file in the build (old-reader.cpp - you only wanted reader.cpp in there really). More conventionally, you list the object files needed for the program so that each object file can be individually rebuilt when needed, and the results linked together. If you get your dependencies correct (a moderately big 'if'), then there's no problem. If you get them wrong, you can end up with inconsistent programs.

However, if the difference is between a 5 second recompile and a 5 minute recompile, you should probably take the 5 minute recompilation (as shown) and answer another SO question while waiting.

To compile on Linux (64-bit):

make CFLAGS="-m64"

To compile on Linux (32-bit):

make CFLAGS="-m32"

To compile on Windows 64:

make PLATFORM=-DWIN64

To compile on Windows 32:

make PLATFORM=-DWIN32

Etc.

_畞蕅 2024-08-17 23:55:09

编译时可以添加-DLINUX=1。

另外,如果运行:

echo "" | cpp -dD

编译时您可以看到默认#define 的列表。 总会有一个:

#define __linux__ 1

在linux中,输出中 。所以如果你通过上面的#define 改变你的LINUX,你不需要做任何特别的事情。即:

...
#elif defined(__linux__)
...

至于 Makefile 本身,我会做类似的事情:

CXX=g++
CPPFLAGS = -I/usr/norton/include
LDFLAGS = -L/usr/norton/lib -lntr
OBJ_FILES = one.o two.o

app: $(OBJ_FILES) Makefile

one.o: one.cpp one.h

two.o: two.cpp two.h

因此使用隐式规则。

You can add -DLINUX=1 when compiling.

Also, if you run:

echo "" | cpp -dD

You can see the list of default #define when compiling. In linux, there will always be a:

#define __linux__ 1

in the output. So if you change your LINUX by the above #define, you don't need to do anything special. Ie:

...
#elif defined(__linux__)
...

As for the Makefile itself, I would do something like:

CXX=g++
CPPFLAGS = -I/usr/norton/include
LDFLAGS = -L/usr/norton/lib -lntr
OBJ_FILES = one.o two.o

app: $(OBJ_FILES) Makefile

one.o: one.cpp one.h

two.o: two.cpp two.h

So the implicit rules are used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文