Revision Difference
Creating_Binary_Modules:_Premake#547591
<cat>Dev</cat>
Binary modules allow you to extend Lua's functionality using C++.
This page will teach you how to set up your IDE for compiling binary modules **using Premake**.
For help on interacting with the C Lua API once set up, refer to <page>C Lua: Functions</page>.
# Dependencies
- Windows: [Visual Studio 2015, Visual Studio 2017 or Visual Studio 2019](https://visualstudio.microsoft.com/vs/older-downloads/) ([Visual Studio 2022](https://visualstudio.microsoft.com/vs/) has given me no issues)
- Linux: [GCC](https://gcc.gnu.org/releases.html) (Included in `sudo apt install build-essential` if your distro supports `apt`)
- macOS: [Xcode](https://apps.apple.com/us/app/xcode/id497799835) (Use the **GCC compiler**) (Untested)
- A copy of the [garrysmod_common](https://github.com/danielga/garrysmod_common) repository
- [Premake5 binaries](https://premake.github.io/download/)
# Project Setup
- Create a new folder, name it anything you like. This will be your project's main folder. Put it somewhere you can get to it in the future.
- Drop your copy of `garrysmod_common` into the project folder. If you downloaded a zip from github, extract its contents into the project folder.
- Drop the Premake5 binaries into the project folder.
- Create a file named `premake5.lua` and paste the following in:
- If you only intend to create server binaries, you can delete the section that says `serverside = false`.
- Replace the name at `CreateWorkspace({name = "example"` with your project name i.e. `name = "myprojectname"`⤶
```
PROJECT_GENERATOR_VERSION = 2
newoption({
trigger = "gmcommon",
description = "Sets the path to the garrysmod_common (https://github.com/danielga/garrysmod_common) directory",
value = "../garrysmod_common"
})
local gmcommon = assert(_OPTIONS.gmcommon or os.getenv("GARRYSMOD_COMMON"),
"you didn't provide a path to your garrysmod_common (https://github.com/danielga/garrysmod_common) directory")
include(gmcommon)
CreateWorkspace({name = "example", abi_compatible = false, path = "projects/" .. os.target() .. "/" .. _ACTION})
CreateProject({serverside = true, source_path = "source", manual_files = false})
IncludeLuaShared()
IncludeScanning()
IncludeDetouring()
IncludeSDKCommon()
IncludeSDKTier0()
IncludeSDKTier1()
CreateProject({serverside = false, source_path = "source", manual_files = true})
IncludeLuaShared()
IncludeScanning()
IncludeDetouring()
IncludeSDKCommon()
IncludeSDKTier0()
IncludeSDKTier1()
filter("system:windows")
files({"source/win32/*.cpp", "source/win32/*.hpp"})
filter("system:linux or macosx")
files({"source/posix/*.cpp", "source/posix/*.hpp"})
```
- Create a folder named `source` and place an empty file `module.cpp` in it. [Copy this file's contents into it.](https://github.com/Facepunch/gmod-module-base/blob/development/examples/HelloWorld/HelloWorld.cpp).
Your project folder should look like this:
<upload src="aad99/8da98ff85ca81b1.png" size="28039" name="explorer_ASnJkJMn31.png" />
In your system's command prompt, navigate to the project folder and run the following:
- Windows: `premake5.exe --os=windows vs2022` (Replace vs2022 with your Visual Studio version i.e. vs2019)
- Linux: `.\premake5 --os=linux gmake2`
- macOS: `.\premake5 --os=macosx xcode4`
- Windows: `premake5.exe --os=windows --gmcommon=../garrysmod_common vs2022` (Replace vs2022 with your Visual Studio version i.e. vs2019)
- Linux: `.\premake5 --os=linux --gmcommon=../garrysmod_common gmake2`
- macOS: `.\premake5 --os=macosx --gmcommon=../garrysmod_common xcode4`
This will create a `projects` folder, which you should navigate to and start working in. If using Visual Studio, your solution file will be in here.
# Naming & Location
The module files should be placed in the garrysmod/lua/bin/ folder.
The names differ between platform and Lua realm.
|File name|Side| require(name) | Platform |
|---|---|---|---|
| `gmsv_example_win32.dll` | Server | example | Windows x32 |
| `gmcl_example_win32.dll` | Client | example | Windows x32 |
| `gmsv_example_win64.dll` | Server | example | Windows x64 (`x86-64` branch is required) |
| `gmcl_example_win64.dll` | Client | example | Windows x64 (`x86-64` branch is required) |
| `gmsv_example_osx.dll` | Server | example | OSX (actually a `.so` file, just renamed) |
| `gmcl_example_osx.dll` | Client | example | OSX (actually a `.so` file, just renamed) |
| `gmsv_example_linux.dll` | Server | example | Linux x32 (actually a `.so` file, just renamed) |
| `gmcl_example_linux.dll` | Client | example | Linux x32 (actually a `.so` file, just renamed) |
| `gmsv_example_linux64.dll` | Server | example | Linux x64 (actually a `.so` file, just renamed; `x86-64` branch is required) |
| `gmcl_example_linux64.dll` | Client | example | Linux x64 (actually a `.so` file, just renamed; `x86-64` branch is required) |
<note>Menu state modules are categorized under `gmsv_name_platform.dll`</note>