premake 在64位Ubuntu系统下编译32位GCC程序

时间:2021-05-23 16:21:17

首先,要安装GCC 4.8, 参考前文:Ubuntu 12.04 & 13.04 安装 GCC4.8.1

其中,重点是安装multilib

apt-get install gcc-4.8-multilib  

接着前文premake管理一个solution和多个project的目录树做一些修改:

1.创建四个configuration,分别为Debu64, Release64, Debug32和Release32, 把它们放在solution下面,这样,每个proejct都共享这些设置。

-- A solution contains projects, and defines the available configurations
solution ("solution2")
configurations {"Debug64", "Release64", "Debug32", "Release32"}
location "build"
targetdir "output" configuration "Debug64"
defines { "DEBUG" }
flags { "Symbols" } configuration "Release64"
defines { "NDEBUG" }
flags { "Optimize" } configuration "Debug32"
linkoptions {"-m32"}
defines { "DEBUG -m32" }
flags { "Symbols" } configuration "Release32"
linkoptions {"-m32"}
defines { "NDEBUG, -m32" }
flags { "Optimize" }

2. 注意上面的32位的配置,添加了linkoptions,并且在defines里面添加了-m32, 这里很别扭。因为-m 在gcc中叫做machine options参数,而premake中没有对应的,只能用defines函数添加。下面这个方式很不好,虽然work.

defines {"DEBUG -m32"}

3. 编译并检查

然后开始运行命令:

/solution2$ premake4 --file=config.lua gmake
Building configurations...
Running action 'gmake'...
Generating build/Makefile...
Generating build/hello1/Makefile...
Generating build/hello2/Makefile...
Done.

进入build目录,开始编译

 cd build/
CHN\shu6889@sloop2:~/work/gitlab/raster/codes/study/premake/solution2/build$ make config=debug32
==== Building hello1 (debug32) ====
Creating ../../output
Creating obj/Debug32
main.cpp
Linking hello1
==== Building hello2 (debug32) ====
Creating obj/Debug32
main.cpp
Linking hello2

现在检查output目录中的两个binary,看看是不是32位。

solution2/build$ cd ../output/
CHN\shu6889@sloop2:~/work/gitlab/raster/codes/study/premake/solution2/output$ file hello1
hello1: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x21747ca29b73061ed5ff3d122c8b2318941bce5d, not stripped
CHN\shu6889@sloop2:~/work/gitlab/raster/codes/study/premake/solution2/output$ file hello2
hello2: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x975e62f065dc80f22aaa22da30b4f65d4a8b2db8, not stripped

编译成功。

4. 现在用clang试一下,添加环境变量:

export CXX=clang++
export CC=clang

重复第3步,成功。