Git Product home page Git Product logo

inspect_cmake_multiplatform's Introduction

cmake_multiplatform

Testing cmake to build a library with linking another external library on multiple platforms Linux, MS Windows, macOS and others.

This is mainly a simple test project for my own to learn how cmake commonly builds on different platforms. Here I found some very useful articles:

On Linux

I first create the external library using bash:

~$ cd linux/  # This is the project source directory
linux$ cmake -S external_lib/ -B external_lib/build/
linux$ cmake --build external_lib/build/

I find the shared library at external_lib/build/. Now build the main program

linux$ cmake -S . -B build
linux$ cmake --build build

We find the shared main program at build/.

linux$ ./build/main
Hello World from external library :-)

On MS Windows

With the Microsoft Visual C Compiler (MSVC) external symbols are not exported by default. We explicitly have to do it with program decorators __declspec(dllexport) on symbols in the library (.dll) and with __declspec(dllimport) on symbols in the main program (.exe), means where the functions from the library are used. I create the external library using the PowerShell:

PS > cd ms-windows\  # This is the project source directory
PS ms-windows> cmake -S .\external_lib\ -B .\external_lib\build\
PS ms-windows> cmake --build .\external_lib\build\ --config Release

I find the shared library (.dll and corresponding .lib) at .\external_lib\build\Release\. Now build the main program

PS ms-windows> cmake -S . -B .\build\
PS ms-windows> cmake --build .\build\ --config Release

I find the shared main program at .\build\Release\ and need to copy the .dll to the main program, otherwise the main program will not find its library. In this case the main program will terminate silently. If you do not get the greeting then look for the .dll:

PS ms-windows> copy .\external_lib\build\Release\hello.dll .\build\Release\
PS ms-windows> .\build\Release\main.exe
Hello World from external library :-)

On Multiplatform build

We have to ensure that both configurations above are running with one cmake setup. The problem is the different behavior with symbol exports of the different compiler. GCC on Linux exports all symbols by default but MSVC on MS Windows does not and need decorators as already described. We solve this with a preprocessor makro as defined in external_lib/hello.h and triggered with a target_compile_definitions() in external_lib/CMakeLists.txt.

Change to the subproject source directory cd multiplatform. Now we can use the commands as shown above for the respective platform with the same cmake multiplatform setup.

inspect_cmake_multiplatform's People

Contributors

ingo-h avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.