diff --git a/.gitignore b/.gitignore index d4fb281..3b5a4e9 100644 --- a/.gitignore +++ b/.gitignore @@ -39,3 +39,15 @@ # debug information files *.dwo + +# Build directory +/build/ + +# IDE files +.vscode/ +.idea/ +*.suo +*.ntvs* +*.njsproj +*.sln +*.vsproj diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4de9c57 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,24 @@ +# CMake minimum version +cmake_minimum_required(VERSION 3.10) + +# Project name +project(MyLibrary) + +# Add the library +add_library(MyLibrary SHARED src/Library.cpp) + +# Define MY_LIB_EXPORT, so that __declspec(dllexport) is used +target_compile_definitions(MyLibrary PRIVATE MY_LIB_EXPORT) + +# Public headers +target_include_directories(MyLibrary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src) + +# Set the output directory for the library +set_target_properties(MyLibrary PROPERTIES + ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin +) + +# Add the examples subdirectory +add_subdirectory(examples) diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..17abba4 --- /dev/null +++ b/build.bat @@ -0,0 +1,5 @@ +@echo off +if not exist build mkdir build +cd build +cmake .. +cmake --build . diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..24897bc --- /dev/null +++ b/build.sh @@ -0,0 +1,6 @@ +#!/bin/bash +set -e +mkdir -p build +cd build +cmake .. +cmake --build . diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 0000000..f8f8864 --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1,5 @@ +# Add the executable +add_executable(Example main.cpp) + +# Link against the library +target_link_libraries(Example MyLibrary) diff --git a/examples/main.cpp b/examples/main.cpp new file mode 100644 index 0000000..78f09ac --- /dev/null +++ b/examples/main.cpp @@ -0,0 +1,8 @@ +#include +#include "Library.h" + +int main() { + int result = add(2, 3); + std::cout << "2 + 3 = " << result << std::endl; + return 0; +} diff --git a/src/Library.cpp b/src/Library.cpp new file mode 100644 index 0000000..aea0f35 --- /dev/null +++ b/src/Library.cpp @@ -0,0 +1,5 @@ +#include "Library.h" + +int add(int a, int b) { + return a + b; +} diff --git a/src/Library.h b/src/Library.h new file mode 100644 index 0000000..89f1463 --- /dev/null +++ b/src/Library.h @@ -0,0 +1,15 @@ +#pragma once + +// Define MY_LIB_EXPORT for exporting symbols from the DLL +#if defined(_WIN32) + #if defined(MY_LIB_EXPORT) + #define MY_LIB_API __declspec(dllexport) + #else + #define MY_LIB_API __declspec(dllimport) + #endif +#else + #define MY_LIB_API +#endif + +// A simple function to be exported +MY_LIB_API int add(int a, int b);