How to compile C in both Linux and Solaris?

人盡茶涼 提交于 2019-12-22 13:59:52

问题


I want to make a Makefile to can compile both in Linux and in Solaris. I know how to do so individually, but how can I combine both and be able to detect what kind of OS I am using? I am trying to do this for just a simple C file - but it is the name of the compiler that changes.


回答1:


GNU Autoconf was designed to solve this very problem:

Autoconf is an extensible package of M4 macros that produce shell scripts to automatically configure software source code packages. These scripts can adapt the packages to many kinds of UNIX-like systems without manual user intervention. Autoconf creates a configuration script for a package from a template file that lists the operating system features that the package can use, in the form of M4 macro calls.

I've used Autoconf myself for a couple of simple projects. You can have a look at the source code to see how to use Autoconf for simple feature detection:

  • count

Note that the purpose of Autoconf is not to detect your operating system. Rather, it's designed to detect features, settings, etc. that may or may not be present on your operating system.

Think of asking the question "what is the C compiler called on this platform?" not "am I running on Linux or Solaris?"




回答2:


I am sure most people will say use some fancy autotools thing. They are right of course.

But... you could put something like the following in your makefile.

OS = `uname -s`
ifeq ($(OS),Linux)
  ...
endif
ifeq ($(OS),Sun)
  ...
endif


来源:https://stackoverflow.com/questions/6488023/how-to-compile-c-in-both-linux-and-solaris

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!