Skip to content

Commit

Permalink
feat: mangled symbols support for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-olivier committed Jul 14, 2024
1 parent 1941174 commit 94ca9dc
Show file tree
Hide file tree
Showing 10 changed files with 363 additions and 204 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include(dylib)
project(dylib VERSION 3.0.0 LANGUAGES CXX)

if(NOT "${CMAKE_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)

Expand Down
60 changes: 30 additions & 30 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
cmake_minimum_required(VERSION 3.14)

project(dylib_example)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})

# dylib fetch

include(FetchContent)

FetchContent_Declare(
dylib
GIT_REPOSITORY "https://github.com/martin-olivier/dylib"
GIT_TAG "v2.2.1"
)

FetchContent_MakeAvailable(dylib)

# build lib.cpp into a shared library

add_library(dynamic_lib SHARED lib.cpp)

# build main.cpp into an executable

add_executable(dylib_example main.cpp)
target_link_libraries(dylib_example PRIVATE dylib)
cmake_minimum_required(VERSION 3.14)

project(dylib_example)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})

# dylib fetch

include(FetchContent)

FetchContent_Declare(
dylib
GIT_REPOSITORY "https://github.com/martin-olivier/dylib"
GIT_TAG "v2.2.1"
)

FetchContent_MakeAvailable(dylib)

# build lib.cpp into a shared library

add_library(dynamic_lib SHARED lib.cpp)

# build main.cpp into an executable

add_executable(dylib_example main.cpp)
target_link_libraries(dylib_example PRIVATE dylib)
258 changes: 129 additions & 129 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,129 +1,129 @@
# dylib example

Here is an example about the usage of the `dylib` library in a project

The functions and variables of our forthcoming dynamic library are located inside [lib.cpp](lib.cpp)

```c++
// lib.cpp

#include <iostream>

#if defined(_WIN32) || defined(_WIN64)
#define LIB_EXPORT __declspec(dllexport)
#else
#define LIB_EXPORT
#endif

extern "C" {

LIB_EXPORT double pi_value = 3.14159;
LIB_EXPORT void *ptr = (void *)1;

LIB_EXPORT double adder(double a, double b) {
return a + b;
}

LIB_EXPORT void print_hello() {
std::cout << "Hello" << std::endl;
}

} // extern "C"
```
The code that will load functions and global variables of our dynamic library at runtime is located inside [main.cpp](main.cpp)
```c++
// main.cpp
#include <iostream>
#include "dylib.hpp"
int main() {
dylib lib("./", "dynamic_lib");
auto adder = lib.get_function<double(double, double)>("adder");
std::cout << adder(5, 10) << std::endl;
auto printer = lib.get_function<void()>("print_hello");
printer();
double pi_value = lib.get_variable<double>("pi_value");
std::cout << pi_value << std::endl;
void *ptr = lib.get_variable<void *>("ptr");
if (ptr == (void *)1) std::cout << 1 << std::endl;
return EXIT_SUCCESS;
}
```

Then, we want a build system that will:

- Fetch `dylib` into the project
- Build [lib.cpp](lib.cpp) into a dynamic library
- Build [main.cpp](main.cpp) into an executable

This build system is located inside [CMakeLists.txt](CMakeLists.txt)

```cmake
# CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(dylib_example)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
# dylib fetch
include(FetchContent)
FetchContent_Declare(
dylib
GIT_REPOSITORY "https://github.com/martin-olivier/dylib"
GIT_TAG "v2.2.1"
)
FetchContent_MakeAvailable(dylib)
# build lib.cpp into a shared library
add_library(dynamic_lib SHARED lib.cpp)
# build main.cpp into an executable
add_executable(dylib_example main.cpp)
target_link_libraries(dylib_example PRIVATE dylib)
```

Let's build our code:
> Make sure to type the following commands inside the `example` folder
```sh
cmake . -B build
cmake --build build
```

Let's run our code:

```sh
# on unix, run the following command inside "build" folder
./dylib_example

# on windows, run the following command inside "build/Debug" folder
./dylib_example.exe
```

You will have the following result:

```sh
15
Hello
3.14159
1
```
# dylib example

Here is an example about the usage of the `dylib` library in a project

The functions and variables of our forthcoming dynamic library are located inside [lib.cpp](lib.cpp)

```c++
// lib.cpp

#include <iostream>

#if defined(_WIN32) || defined(_WIN64)
#define LIB_EXPORT __declspec(dllexport)
#else
#define LIB_EXPORT
#endif

extern "C" {

LIB_EXPORT double pi_value = 3.14159;
LIB_EXPORT void *ptr = (void *)1;

LIB_EXPORT double adder(double a, double b) {
return a + b;
}

LIB_EXPORT void print_hello() {
std::cout << "Hello" << std::endl;
}

} // extern "C"
```
The code that will load functions and global variables of our dynamic library at runtime is located inside [main.cpp](main.cpp)
```c++
// main.cpp
#include <iostream>
#include "dylib.hpp"
int main() {
dylib lib("./", "dynamic_lib");
auto adder = lib.get_function<double(double, double)>("adder");
std::cout << adder(5, 10) << std::endl;
auto printer = lib.get_function<void()>("print_hello");
printer();
double pi_value = lib.get_variable<double>("pi_value");
std::cout << pi_value << std::endl;
void *ptr = lib.get_variable<void *>("ptr");
if (ptr == (void *)1) std::cout << 1 << std::endl;
return EXIT_SUCCESS;
}
```

Then, we want a build system that will:

- Fetch `dylib` into the project
- Build [lib.cpp](lib.cpp) into a dynamic library
- Build [main.cpp](main.cpp) into an executable

This build system is located inside [CMakeLists.txt](CMakeLists.txt)

```cmake
# CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(dylib_example)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
# dylib fetch
include(FetchContent)
FetchContent_Declare(
dylib
GIT_REPOSITORY "https://github.com/martin-olivier/dylib"
GIT_TAG "v2.2.1"
)
FetchContent_MakeAvailable(dylib)
# build lib.cpp into a shared library
add_library(dynamic_lib SHARED lib.cpp)
# build main.cpp into an executable
add_executable(dylib_example main.cpp)
target_link_libraries(dylib_example PRIVATE dylib)
```

Let's build our code:
> Make sure to type the following commands inside the `example` folder
```sh
cmake . -B build
cmake --build build
```

Let's run our code:

```sh
# on unix, run the following command inside "build" folder
./dylib_example

# on windows, run the following command inside "build/Debug" folder
./dylib_example.exe
```

You will have the following result:

```sh
15
Hello
3.14159
1
```
38 changes: 19 additions & 19 deletions example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include <iostream>
#include "dylib.hpp"

int main() {
dylib lib("./", "dynamic_lib");

auto adder = lib.get_function<double(double, double)>("adder");
std::cout << adder(5, 10) << std::endl;

auto printer = lib.get_function<void()>("print_hello");
printer();

double pi_value = lib.get_variable<double>("pi_value");
std::cout << pi_value << std::endl;

void *ptr = lib.get_variable<void *>("ptr");
if (ptr == (void *)1) std::cout << 1 << std::endl;

return EXIT_SUCCESS;
#include <iostream>
#include "dylib.hpp"

int main() {
dylib lib("./", "dynamic_lib");

auto adder = lib.get_function<double(double, double)>("adder");
std::cout << adder(5, 10) << std::endl;

auto printer = lib.get_function<void()>("print_hello");
printer();

double pi_value = lib.get_variable<double>("pi_value");
std::cout << pi_value << std::endl;

void *ptr = lib.get_variable<void *>("ptr");
if (ptr == (void *)1) std::cout << 1 << std::endl;

return EXIT_SUCCESS;
}
Loading

0 comments on commit 94ca9dc

Please sign in to comment.