premake在Ubuntu和GCC环境下创建简单的C++工程

时间:2021-11-15 17:13:51

由于premake基于lua脚本,为了方便编辑lua脚本,我在emacs24中利用package system安装了lua-mode。

然后创建config.lua文件,填入下面这段,主要来自:http://industriousone.com/basic-script

-- 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" }

创建main.cpp文件,很简单:

#include <iostream>
using namespace std; int main(void) {
cout << "hello1" << endl;
}

运行premake命令产生makefile

premake_study/hello1$ premake4 --file=config.lua --os=linux --platform=x64 gmake
Building configurations...
Running action 'gmake'...
Generating Makefile...
Generating Hello1.make...
Done.

现在执行make命令:

 make
==== Building Hello1 (debug) ====
Creating obj/Debug
main.cpp
Linking Hello1

创建了一个obj/Debug目录树,

premake_study/hello1$ tree
.
├── config.lua
├── Hello1
├── Hello1.make
├── main.cpp
├── Makefile
└── obj
└── Debug
├── main.d
└── main.o

运行Hello1, 成功打印"hello1" 字符串。

看一下产生的Makefile文件内容,会知道如何使用。

# GNU Make solution makefile autogenerated by Premake
# Type "make help" for usage help ifndef config
config=debug
endif
export config PROJECTS := Hello1 .PHONY: all clean help $(PROJECTS) all: $(PROJECTS) Hello1:
@echo "==== Building Hello1 ($(config)) ===="
@${MAKE} --no-print-directory -C . -f Hello1.make clean:
@${MAKE} --no-print-directory -C . -f Hello1.make clean help:
@echo "Usage: make [config=name] [target]"
@echo ""
@echo "CONFIGURATIONS:"
@echo " debug"
@echo " release"
@echo " debug64"
@echo " release64"
@echo ""
@echo "TARGETS:"
@echo " all (default)"
@echo " clean"
@echo " Hello1"
@echo ""
@echo "For more information, see http://industriousone.com/premake/quick-start"

好了,默认是Debug版本,现在运行命令,可以创建release版本。

premake_study/hello1$ make config=release all
==== Building Hello1 (release) ====
Creating obj/Release
main.cpp
Linking Hello1

还可传递参数 config=release64和config=debug64. 咋一看比cmake贴心好用。

不过以为之前运行premake4命令传递参数是--platform=x64, 所以这里make传递的config不管是什么都编译出64bit bianry. 用file命令可以查看之:

 file Hello1
Hello1: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0xdef2bc128f21a691fa196dc20c50b7ad4dc56d0f, stripped

premake可以用来清理产生的make file

premake_study/hello1$ premake4 --file=config.lua clean
Building configurations...
Running action 'clean'...
Done.

不过即便我这里premake4用了参数--platform=x32,编译出来的还是64bit的binary。

还不是很清楚原因。

如果想要详细的看到编译的执行过程,和CMake使用方式一样,在make的时候加参数verbose=1

premake_study/hello1$ make config=release verbose=1 all
==== Building Hello1 (release) ====
Creating obj/Release
mkdir -p obj/Release
main.cpp
g++ -MMD -MP -DNDEBUG -O2 -o "obj/Release/main.o" -MF obj/Release/main.d -c "main.cpp"
Linking Hello1
g++ -o ./Hello1 obj/Release/main.o -s