Skip to content

Commit d42e126

Browse files
committed
Add content for 'C++ and Lua' article
1 parent dad51ce commit d42e126

File tree

33 files changed

+949
-0
lines changed

33 files changed

+949
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44
/lua
55
/*.rock
66
/*.sublime-workspace
7+
8+
build
79
profile.json

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "src/cpp/cmake_git_submodule/lua"]
2+
path = src/cpp/cmake_git_submodule/lua
3+
url = https://github.com/lua/lua.git

src/cpp/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Lua with C++
2+
3+
This folder contains a set of examples from my article [C++ and Lua](https://martin-fieber.de/blog/cpp-and-lua) as part of the [Lua series](https://martin-fieber.de/series/lua).
4+
5+
## Examples
6+
7+
| Folder | Contents |
8+
| -------------------------- | --------------------------------------------------- |
9+
| `cmake_find_package/` | Include Lua in a project via CMake `find_package` |
10+
| `cmake_external_project/` | Include Lua in a project via CMake external project |
11+
| `cmake_git_submodule/` | Include Lua in a project via Git submodule |
12+
| `example-read-config/` | All about reading data from Lua |
13+
| `example-functions/` | Calling Lua functions from C++ |
14+
| `example-cpp-to-lua/` | Calling a C++ from Lua |
15+
| `example-custom-module` | Define a custom Lua module from C++ |
16+
| `example-override-builtin` | Override built-in Lua functions |
17+
18+
## Setup
19+
20+
Every example is set up the same, using CMake to build. Setting any example folder as current working directory, run the following command to create the CMake build configuration.
21+
22+
```shell
23+
cmake -B build
24+
```
25+
26+
The next command will build the executable into the folder `build`.
27+
28+
```shell
29+
cmake --build build
30+
```
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
3+
# Setup a CMake project for C++
4+
project(
5+
LuaWithCMakeExternalProject
6+
VERSION 1.0
7+
LANGUAGES CXX)
8+
9+
# Add an executable based on one source file
10+
add_executable(ProgramName main.cpp)
11+
12+
# Include the fetch content module
13+
include(FetchContent)
14+
15+
# Fetch Lua from git mirror
16+
FetchContent_Declare(
17+
lua
18+
GIT_REPOSITORY "https://github.com/lua/lua.git"
19+
GIT_TAG v5.5.0
20+
)
21+
22+
# Make content available
23+
FetchContent_MakeAvailable(lua)
24+
25+
# Setup Lua library target with include directories
26+
add_library(Lua::Lua INTERFACE IMPORTED)
27+
target_include_directories(
28+
Lua::Lua
29+
INTERFACE "${lua_SOURCE_DIR}")
30+
31+
# Link Lua library to the executable
32+
target_link_libraries(
33+
ProgramName
34+
PRIVATE Lua::Lua)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Lua, CMake, FetchContent
2+
3+
Using Lua with CMake and the `FetchContent` module. Lua will be fetched from source.
4+
5+
## Setup
6+
7+
With the folder containing this file as current working directory, run the following command to create the CMake build configuration.
8+
9+
```shell
10+
cmake -B build
11+
```
12+
13+
The next command will build the executable into the folder `build`.
14+
15+
```shell
16+
cmake --build build
17+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <lua.hpp>
2+
3+
int main() {
4+
// More to come ...
5+
6+
return 0;
7+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
3+
# Setup a CMake project for C++
4+
project(
5+
LuaWithCMakeFindPackage
6+
VERSION 1.0
7+
LANGUAGES CXX)
8+
9+
# Add an executable based on one source file
10+
add_executable(ProgramName main.cpp)
11+
12+
# Try to find the Lua package on the system
13+
find_package(Lua)
14+
15+
# If Lua was found and target is not set up
16+
if(Lua_FOUND AND NOT TARGET Lua::Lua)
17+
# Setup Lua library target
18+
add_library(Lua::Lua INTERFACE IMPORTED)
19+
20+
# Set include directories
21+
target_include_directories(
22+
Lua::Lua
23+
INTERFACE "${LUA_INCLUDE_DIR}")
24+
endif()
25+
26+
# Link Lua library to the executable
27+
target_link_libraries(
28+
ProgramName
29+
PRIVATE Lua::Lua)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Lua, CMake, find_package
2+
3+
Using Lua with CMake and find_package. This requires Lua to already be installed/available on the system.
4+
5+
## Setup
6+
7+
With the folder containing this file as current working directory, run the following command to create the CMake build configuration.
8+
9+
```shell
10+
cmake -B build
11+
```
12+
13+
The next command will build the executable into the folder `build`.
14+
15+
```shell
16+
cmake --build build
17+
```
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <lua.hpp>
2+
3+
int main() {
4+
// More to come ...
5+
6+
return 0;
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
3+
# Setup a CMake project for C++
4+
project(
5+
LuaWithGitSubmodules
6+
VERSION 1.0
7+
LANGUAGES CXX)
8+
9+
# Add an executable based on one source file
10+
add_executable(ProgramName main.cpp)
11+
12+
# Setup Lua library target with include directories
13+
add_library(Lua::Lua INTERFACE IMPORTED)
14+
target_include_directories(
15+
Lua::Lua
16+
INTERFACE "lua")
17+
18+
# Link Lua library to the executable
19+
target_link_libraries(
20+
ProgramName
21+
PRIVATE Lua::Lua)

0 commit comments

Comments
 (0)