From bbd66503d81b6ee8509d0c0604128d7a3acca81a Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 4 Sep 2024 13:09:34 +0200 Subject: add dynamic linking test (tested on linux) --- mwe/dynlink/exec/CMakeLists.txt | 23 +++++++++++++++++++++++ mwe/dynlink/exec/main.c | 8 ++++++++ mwe/dynlink/exec/makefile | 1 + mwe/dynlink/lib/CMakeLists.txt | 10 ++++++++++ mwe/dynlink/lib/lib.c | 6 ++++++ mwe/dynlink/lib/lib.h | 4 ++++ mwe/dynlink/lib/makefile | 1 + mwe/dynlink/readme.md | 1 + 8 files changed, 54 insertions(+) create mode 100644 mwe/dynlink/exec/CMakeLists.txt create mode 100644 mwe/dynlink/exec/main.c create mode 100644 mwe/dynlink/exec/makefile create mode 100644 mwe/dynlink/lib/CMakeLists.txt create mode 100644 mwe/dynlink/lib/lib.c create mode 100644 mwe/dynlink/lib/lib.h create mode 100644 mwe/dynlink/lib/makefile create mode 100644 mwe/dynlink/readme.md diff --git a/mwe/dynlink/exec/CMakeLists.txt b/mwe/dynlink/exec/CMakeLists.txt new file mode 100644 index 0000000..5335f0f --- /dev/null +++ b/mwe/dynlink/exec/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.28) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 20) + +project(main C CXX) + +# Since we have the source code for the test library, we might as well let +# CMake use it so it automatically picks up the interface headers and compiles +# the library for us. The same can be achieved manually, but is more code. +add_subdirectory(../lib test) + +add_executable(main main.c) + +# Make sure ld.so (linux) looks in the same folder as the final executable for +# the .so dependency +set_target_properties(main PROPERTIES BUILD_RPATH "$ORIGIN") + +# This links test (dynamically, as defined in ../lib/CMakeLists.txt) and makes +# the interface header available (also as defined in +# ../lib/CMakeLists.txt) +target_link_libraries(main PRIVATE test) + diff --git a/mwe/dynlink/exec/main.c b/mwe/dynlink/exec/main.c new file mode 100644 index 0000000..e3f458a --- /dev/null +++ b/mwe/dynlink/exec/main.c @@ -0,0 +1,8 @@ +#include + +int main() { + library_function(); + + return 0; +} + diff --git a/mwe/dynlink/exec/makefile b/mwe/dynlink/exec/makefile new file mode 100644 index 0000000..eec7c61 --- /dev/null +++ b/mwe/dynlink/exec/makefile @@ -0,0 +1 @@ +include ../../../lazy.mk diff --git a/mwe/dynlink/lib/CMakeLists.txt b/mwe/dynlink/lib/CMakeLists.txt new file mode 100644 index 0000000..2d15353 --- /dev/null +++ b/mwe/dynlink/lib/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.28) + +set(CMAKE_C_STANDARD 11) +set(CMAKE_CXX_STANDARD 20) + +project(lib C CXX) + +add_library(test SHARED lib.c) +target_include_directories(test SYSTEM INTERFACE .) + diff --git a/mwe/dynlink/lib/lib.c b/mwe/dynlink/lib/lib.c new file mode 100644 index 0000000..930e403 --- /dev/null +++ b/mwe/dynlink/lib/lib.c @@ -0,0 +1,6 @@ +#include + +void library_function() { + printf("%s\n", __PRETTY_FUNCTION__); +} + diff --git a/mwe/dynlink/lib/lib.h b/mwe/dynlink/lib/lib.h new file mode 100644 index 0000000..068f07d --- /dev/null +++ b/mwe/dynlink/lib/lib.h @@ -0,0 +1,4 @@ +#pragma once + +void library_function(); + diff --git a/mwe/dynlink/lib/makefile b/mwe/dynlink/lib/makefile new file mode 100644 index 0000000..eec7c61 --- /dev/null +++ b/mwe/dynlink/lib/makefile @@ -0,0 +1 @@ +include ../../../lazy.mk diff --git a/mwe/dynlink/readme.md b/mwe/dynlink/readme.md new file mode 100644 index 0000000..0826951 --- /dev/null +++ b/mwe/dynlink/readme.md @@ -0,0 +1 @@ +# dynamic linking test -- cgit v1.2.3