premake基本配置详解

Posted csfreebird

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了premake基本配置详解相关的知识,希望对你有一定的参考价值。

前文premake在Ubuntu和GCC环境下创建简单的C++工程中举了一个例子,虽然能work,但是并不是很了解。后来花了点时间看了lua的语法,自以为理解了,再回头来看,发现还是有些不懂。现在明白了,premake虽然基于lua,但是也有自己的一些语法。标准的lua语法在premake中是肯定可以用的。

premake自己的语法要参考文档:http://industriousone.com/scripting-reference

现在来细说一下原来的例子中的脚本。

-- A solution contains projects, and defines the available configurations
solution "Hello1"
configurations  "Debug", "Release" 

-- A project defines one build target
project "Hello1"
kind "ConsoleApp"
language "C++"
files  "**.h", "**.cpp" 

configuration "Debug"
defines  "DEBUG" 
flags  "Symbols" 

configuration "Release"
defines  "NDEBUG" 
flags  "Optimize" 

这里一篇文档可以快速的覆盖上面的用法: http://industriousone.com/configurations-0

注意:

1. 这些solution, configurations等都是function, 并不是变量,这里和lua标准语法不一样(标准写法也可以),而是用premake自定义的规则,有空格,有 号等。

2. solution可以包含多个project,语法参考http://industriousone.com/solution,solution管理多个project,这个设计很实用。

3. configurations接受的参数是每一段configuration的名字,自己应该在每个project中定义对应的configuration的设置。必须在所有project定义前调用configurations。

4. configurations函数的参数,Debug放在最前面,也就意味着将来产生的*.make文件中,Debug会作为默认设置。如果现在将Release放在最前面,生成make文件后,会看到:

ifndef config
  config=release
endif
当make不传递参数时,就会默认使用release配置。

5. project里面可以通过configuration进行设置 ,文档:http://industriousone.com/project,里面有错误:

solution "MySolution"
   configuration  "Debug", "Release"  // should be configurations
 
   project "MyProject"

6. 注意kind,即可用在configuration里面,也可用在project里面。文档: http://industriousone.com/kind

7. configuration就是对应3中的配置函数。注意,可以用通配符,比如"*Lib"或则“*DLL”来匹配configurations中的参数。configuration函数之后所有的配置都属于这个configuration,直到遇到下一个configuration为止。在我的例子中,后续的只有defines和flags函数。文档:http://industriousone.com/configuration

8. defines用来给一个project添加预处理或者编译符号。前面两个configuration里面分别用来一次define,看一下:

configuration "Debug"
defines  "DEBUG" 

configuration "Release"
defines  "NDEBUG" 
结果在生成的Hello1.make文件中,我们看到生成了对应的gmake指令:-DDEBUG和-DNDEBUG

ifeq ($(config),debug)
  OBJDIR     = obj/Debug
  TARGETDIR  = .
  TARGET     = $(TARGETDIR)/Hello1
  DEFINES   += -DDEBUG

ifeq ($(config),release)
  OBJDIR     = obj/Release
  TARGETDIR  = .
  TARGET     = $(TARGETDIR)/Hello1
  DEFINES   += -DNDEBUG

9. flags用来指定编译或者链接过程中的标识,查看文档: http://industriousone.com/flags

我这里用了最普通的, "Symbols" 允许编译出调试信息,"Optimize"编译优化,一般为Release准备。 


10. premake指令可以简化为:

premake4 --file=config.lua gmake  
原来的
premake4 --file=config.lua --os=linux --platform=x64 gmake  
有副作用。特别是--platform=x64会导致产生的make文件中有debug64和release64的配置。还是由自己控制的比较好。

11. 最后介绍一个添加文件和目录,甚至排除某些文件的用法。参考:http://industriousone.com/adding-files

有了这个,就没必要像一般的makefile每个目录都写makefile,和cmake差不多方便了。







以上是关于premake基本配置详解的主要内容,如果未能解决你的问题,请参考以下文章

我可以用 premake 打印最终组装的 gcc 命令吗?

Premake 生成的解决方案不会在 Release 中编译,但在 Debug 中编译

为什么我不选择cmake,而是premake

OpenGL SDK如何使用Premake创建新项目

MyBatis从入门到入土——使用详解

iOS PhotoKit框架 详解