Shared Objects In FreeCAD

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Zig_mint
Posts: 12
Joined: Thu Jan 10, 2019 5:48 pm

Shared Objects In FreeCAD

Postby Zig_mint » Mon Mar 25, 2019 11:12 am

Hi,

I have been struggling to understand the shared objects that are made by FreeCAD after compilation.
I wanted to know how can I write code so that when I compile the source code I can use the shared objects for my workbench.

For Example, the FEM workbench has two .so files namely Fem.so and FemGui.so I want to make similar files for my workbench and later use them, any suggestions hints or comments are most welcome.

Thank You
ezzieyguywuf
Posts: 637
Joined: Tue May 19, 2015 1:11 am

Re: Shared Objects In FreeCAD

Postby ezzieyguywuf » Mon Mar 25, 2019 12:31 pm

Zig_mint wrote:
Mon Mar 25, 2019 11:12 am
I want to make similar files for my workbench and later use them, any suggestions hints or comments are most welcome.
You wish to use these library files within FreeCAD? Or elsewhere? Where in FreeCAD - in your same Workbench, or are you attempting to extend another existing workbench?

How familiar are you with cmake? It is the build system that FreeCAD uses. Within cmake, there are identified what are referred to as "targets", which are the various components of the program that are compiled into libraries and executables. In most directories, you'll find a CMakeLists.txt which is the cmake configuration file - for example, in src/Mod/Fem/App/CMakeLists.txt, near the bottom, you'll find the line:

Code: Select all

add_library(Fem SHARED ${Fem_SRCS})
This tells cmake to generate a target called Fem and to compile a shared library for it - that library will be named something like libFem.so on linux.

Therefore, if you wish to utilize this Fem library in your workbench, it would be a simple matter of importing any necessary header files, and then linking the Fem library in your own cmake configuration:

Code: Select all

#src/Mod/MyModule/App/SomeCode.cpp
#include <Mod/App/Fem/App/FemMesh.h
Fem::Mesh myMesh;
myMesh.getFacesOnly();
etc...

Code: Select all

#src/Mod/MyModule/App/CMakeLists.txt
add_library(MyWorkbench SHARED SomeCode.cpp)
target_link_library(MyWorkbench Fem)
If instead you're trying to use the library outside of FreeCAD, things become a bit more complicated.