Setup a C++ Project using Premake

AyoubGharbi | Jan 16, 2022 min read

Setting up a C++ project using Premake can be a bit tricky, but once you understand the basics, it’s a quick and easy process. In this blog post, we’ll go over the steps you need to take to create a new C++ project using Premake.

1- Install Premake

The first thing you need to do is install Premake. You can download the latest version from the Premake website. Once you’ve downloaded and added it to your enviornment PATH, you should be able to run the “premake5” command from the command line.

2- Create a new project directory

Next, create a new directory for your project. This is where you’ll store all of your project files, including your Premake script.

3- Create a Premake script

In the root of your project directory, create a new file called “premake5.lua”. This is the script that Premake will use to generate your project files.

4- Define your project

In your Premake script, use the “workspace” and “project” functions to define your project. For example:

workspace "MyProject"
   configurations { "Debug", "Release" }

project "MyProject"
   kind "ConsoleApp"
   language "C++"
   targetdir "bin/%{cfg.buildcfg}"

5- Define your source files

Use the “files” function to define the source files for your project. For example:

files { "src/**.h", "src/**.cpp" }

This tells Premake to include all files with the .h and .cpp extensions in the “src” directory and its subdirectories.

6- Generate project files

Once you’ve defined your project and source files, run the “premake5” command followed by your desired format. For example, if you want to generate Visual Studio project files, you would run:

premake5 vs2022

This will generate the necessary project files for Visual Studio 2022.

7- Open your project

You can now open your project in your preferred IDE (e.g. Visual Studio) and start coding.

8- Add external libraries (optional)

If you’re using external libraries, you’ll need to add them to your Premake script. You can do this using the “includedirs” and “links” functions. For example:

includedirs { "path/to/include" }
links { "libraryname" }

This tells Premake to include the “path/to/include” directory when searching for headers, and to link against the “libraryname” library.

That’s it! Now you have a simple c++ project using Premake. It allows you to easily generate project files for various IDEs, making it a great tool for cross-platform development.