Windows 开发、Linux 生产,是一套综合评估下较为合理的工作方式。premake5 可通过 Lua 脚本配置,自动化生成 Visual Studio 工程与 gmake 工程,方便在 Linux 和 Windows 两端进行开发和编译。

下面是一个完整示例,解决方案名为 server,包含两个工程:stick_engine(生成静态库)和 app(生成可执行文件)。

--[[
                premake5.lua
                	windows: premake5.exe --os=windows vs2015
                	linux:  premake5.exe --os=linux gmake
                ]]
                
                workspace "server"
                	location (_ACTION)  -- subdir vs2015 (or gmake, ...)
                	configurations { "Release", "Debug" }
                	platforms { "x64"}
                	targetdir "../bin/%{cfg.platform}/%{cfg.buildcfg}"
                	language "C++"
                	flags {
                		"C++11",
                		"StaticRuntime",
                	}
                	includedirs {
                		"../src/stick_engine",
                		"../src/stick_engine/3rd",
                		"../src/app",
                		"../deps/include",
                		"../external/include",
                		"../external/include/PhysX",
                	}
                	links {
                		"stick_engine",
                		"hiredis",
                	}
                
                	filter "configurations:Debug"
                		flags { "Symbols" }
                		defines { "DEBUG" }
                	filter "configurations:Release"
                		defines { "NDEBUG" }
                		optimize "Full"
                	filter {}
                
                	if os.is("windows") then
                		defines {
                			"_WIN32_WINNT=0x0600",  -- i.e. Windows 7 target
                			"WIN32_LEAN_AND_MEAN",  -- To fix error :  WinSock.h has already been included
                			"NOMINMAX",  -- To disable windows.h min/max.
                			"_CRT_SECURE_NO_WARNINGS",  -- for strerror()
                		}
                		libdirs {
                			"../deps/lib_win64",
                
                		}
                		links {
                			"libeay32",
                			"ws2_32",
                		}
                		buildoptions { "/MP" }
                		filter "configurations:Debug"
                			libdirs { "../deps/lib_win64/Debug" }
                			links {
                				"libprotobufd",
                			}
                		filter "configurations:Release"
                			libdirs { "../deps/lib_win64/Release" }
                			links {
                				"libprotobuf",
                			}
                			linkoptions { "/SAFESEH:NO" }
                		filter {}
                
                	elseif os.is("linux") then
                		includedirs {
                		}
                		linkoptions { "-Wl,-E" }--
                		libdirs { "../deps/lib_centos" }
                		links {
                			"crypt",
                			"pthread",
                			"crypto",
                			"expat",
                			"sasl2",
                			"ssl",
                			"protobuf",
                			"tcmalloc_and_profiler",
                			"unwind",
                			"dl",
                			"m",
                			"rt",
                
                			"PhysX3_x64",
                			"PhysX3Extensions",
                			"PhysX3Common_x64",
                			"PhysX3Cooking_x64",
                			"PxFoundation_x64",
                			"PxPvdSDK_x64",
                
                		}
                		filter "configurations:Debug"
                			links {
                
                			}
                		filter "configurations:Release"
                			links {
                
                			}
                	end
                
                project "stick_engine"
                	kind "StaticLib"
                	files {
                		"../src/stick_engine/**",  -- *.cpp;h
                	}
                
                	if os.is("windows") then
                		includedirs {
                		}
                		buildoptions { "/MP" }
                	else
                		includedirs {
                		}
                
                		removefiles {
                			"../src/stick_engine/3rd/lua/*.cpp",
                			"../src/stick_engine/3rd/lua/*.h",
                			"../src/stick_engine/3rd/lua/*.c",
                			"../src/stick_engine/3rd/lua/*.hpp",
                
                			"../src/stick_engine/3rd/vld/*.cpp",
                			"../src/stick_engine/3rd/vld/*.h",
                			"../src/stick_engine/3rd/vld/*.c",
                			"../src/stick_engine/3rd/vld/*.hpp",
                
                			"../src/stick_engine/3rd/qiniu/*.cpp",
                			"../src/stick_engine/3rd/qiniu/*.h",
                			"../src/stick_engine/3rd/qiniu/*.c",
                			"../src/stick_engine/3rd/qiniu/*.hpp",
                		}
                
                	end
                	removelinks { "stick_engine" }
                	removelinkoptions { "/SAFESEH:NO" }
                
                project "app"
                	kind "ConsoleApp"
                	targetname "stick_engine_app"
                	files {
                		"../src/app/**",  -- *.cpp;h
                
                	}
                	includedirs {
                		"../src/app",
                	}
                
                	if os.is("windows") then
                		includedirs {
                
                		}
                	else
                		includedirs {
                
                		}
                
                		removefiles {
                			"../src/stick_engine/3rd/lua/*.cpp",
                			"../src/stick_engine/3rd/lua/*.h",
                			"../src/stick_engine/3rd/lua/*.c",
                			"../src/stick_engine/3rd/lua/*.hpp",
                
                			"../src/stick_engine/3rd/vld/*.cpp",
                			"../src/stick_engine/3rd/vld/*.h",
                			"../src/stick_engine/3rd/vld/*.c",
                			"../src/stick_engine/3rd/vld/*.hpp",
                
                			"../src/stick_engine/3rd/qiniu/*.cpp",
                			"../src/stick_engine/3rd/qiniu/*.h",
                			"../src/stick_engine/3rd/qiniu/*.c",
                			"../src/stick_engine/3rd/qiniu/*.hpp",
                
                		}
                
                
                	end
                

在 Linux 下,进入 gmake 目录后直接执行 make 即可完成编译。