Hopsan
Compiling the Component Library

When your code is finished, the project must be compiled. It is necessary to use the same compiler that your version of Hopsan was compiled with. The project must also be compiled in release mode unless you are building against a debug compiled version of the HopsanCore in debug mode.

There are two ways of compiling your library:
The simple way:

  1. Install the compiler according to How to setup the component library development environment
  2. Make sure that your library has a libraryinformation .xml file according to The Hopsan Component Library Information File At least the lib and source elements must be present. You may also need additional build flags if your library depends on third party code.
    Note! The build flags for linking against HopsanCore are added automatically during the build process.
  3. When you load the library in HopsanGUI for the first time it will be compiled. It will also be automatically recompiled if the library fails to load. This happens if you have updated Hopsan to a new version. A window will notify you of any compilation warnings or errors.
  4. If you make any changes to your component library code, you must remeber to manually recompile the library from the right-click menu in the library widget. If you get errors during compilation, you must fix them before the library can be used. You should also strive to fix any warnings generated by your code. They indicate that something is not quite right and while to code "works" you may not get the expected result.

The less simple way:

  1. Use an external build environment, such as the QtCreator IDE and qmake. See the instructions for installing the development environment page_setupbuildenvironment .
    You only need to install the Qt Creator IDE and the Qt libraries. The external build tools are only be necessary if you want to recompile Hopsan itself.
  2. See the exampleComponentLibrary files for examples on how to write the Qt project file. You must add the include path to Hopsan/HopsanCore/include and the linker path to Hopsan/bin. You must use the intended Hopsan version, so if you have multiple versions installed in different places you must change these when switching version.
  3. Note! if the library information file has a lib and source element you can still (re)compile the library from inside HopsanGUI as well.

Compiling and Linking:
The compilation goes through two phases, first the .cpp file(s) are compiled and then they are linked into a shared object .dll, .so or .dynlib file that will appear in your library directory.

If something is wrong in your code you may get compilation errors in the first phase, you will need to correct those before the linking step can proceed. You should also pay attention to warnings, they may indicate that your code will not produce the expected result. The error and warning messages are usually quite descriptive and points to the line where the problem comes from.

You may also get linker errors which are a bit more difficult to understand if you are not used to compiling you own code. If you get something similar to the following error message: "cannot find -lhopsancore" you have probably set the LIBS path incorrectly in your Qt project file. It should point to where the HopsanCore .dll, .so, .dynlib file is located. Also specify the correct release or debug HopsanCore file -lhopsancore or -lhopsancore_d

ToDo: This section should list common compilation and linking errors and who to solve them