Sharing code among multiple projects

Normally your projects are independent of each other. But there are certainly times when you may have common project items that you want to share amongst multiple projects.

Copy and paste with a master project

The simplest way to do this is with copy and paste. You can select the project items in the Navigator, copy them and then paste them into another project. This creates two separate copies of the project items for each project. The best way to use this technique is with a “master project” that contains the shared code you use. Keep the master project updated with all your shared code and any changes you make.

You can copy shared project items from the master project as needed to your other projects. This technique allows you to re-use project items without having to worry about changes affecting other projects.

The downside to this approach is that you make fixes or enhances to the code in your master projects, then the projects you copied it to will not benefit from the change unless you manually copy the changes over.

Export / import items

You can also use the File Export and Import items to create standalone files on disk that can be used to share with others and with other projects. Project Items can be exported as Binary or XML format. To export a project item, select it in the Navigator and then choose File > Export from the menu. You'll be prompted for a location.

To import a project item that was previously exported, choose File > Import from the menu.

When you import an item exported in this way, it becomes part of the new project and is not affected by changes to the original.

External items

The last option is to convert project items to “External Project Items”. An External Project Item can appear in multiple projects, so changing the shared project item in one project changes it for all projects that use it. When you create an External Project Item, the item becomes a file on disk (in either Binary or XML format).

To convert a normal project item to an External Project Item, open the contextual menu for the item in the Navigator (usually by right-clicking on it) and select “Make External".

../../_images/sharing_code_among_multiple_projects_externalprojectitem.png

This prompts you to choose a location and type for the file. Your choices are Binary or XML. If your External Project Item is going to be stored in a version control system, XML is the better choice. External Project Items appear in the Navigator as italic with a small arrow icon superimposed on the normal icon.

Once an you have a project item saved as a file on disk you can add it to another project as an external item. To do so, hold down the Alt key ( Option on Mac) before clicking in the menu, then select the File menu and you'll see the Import item now says "Import External". Choose that command and select the file to be imported.

You can also add an external item by dragging the file from the disk to the Navigator while holding down Option (on Mac) or Shift Ctrl (on Windows and Linux).

Files that were created using the standard "export" process can also be added as external using the above steps.

Warning

If you make a change to an external project item outside of Xojo (perhaps with version control), the change is not automatically reflected in any open projects that are using the external project item. You have to close and reopen the open projects in order to see the change.

Libraries

Important

Using Libraries requires Xojo 2025r3 or later.

A Library is a group of project items that typically support a common purpose. Perhaps you have a set of classes that manipulate images but you also have user interface elements such as a window that provides additional functionality. You might even have some example images that are displayed in the window. All of this and more can be included in a Library. In fact, a Library can include any project items you wish.

When you build a project that includes a Library, the items in the Library will be compiled to machine code and then moved into a separate file that can then be distributed to others. Your Library will include project item names and all member (method, event, property, etc.) names as these are needed for auto-complete in the Code Editor but your source code is not included in the built Library file.

A Library is a great way to distribute functionality where you want projects to use a specific version of your functions until your improved versions are ready. Libraries are also a great way to distribute functionality to other Xojo users either for free or for sale while protecting your source code since it is not included.

Creating a Library

To create a Library in an existing project:

  1. Open a project or create a new one. This project will contain the source for your Library.

  2. Choose Insert > Library.

  3. Use the Inspector to give your Library an appropriate name. This is the name that the library file will have once built. The file extension .xojo_library will be added once it is built.

  4. Drag project items you wish included into the Library item in the Navigator.

  5. Save your project. Xojo will not build the Library until its source project is saved.

  6. Run your project to make sure the Library works as expected. Your source project should include code allows you to test the items in the Library.

  7. Build your project. This will build the app for your project as well as the Library file.

A project can contain only one Library. To create multiple Libraries, create multiple projects.

Make sure to select all the platforms you wish to support (in Build Settings) before building your Library.

The following information from the Shared Build Settings will be included in your built Library. Users can view this information by opening the Xojo IDE about box and then clicking on Plugins and Libraries.

  • Version

  • Copyright

  • Description

  • Build Date

  • IDE Version used to build

Important

Libraries only support 64-bit builds.

Using a Library

To make a Library available to a specific project:

  1. Drag the compiled library file (the file with the .xojo_library file extension) into the folder with your project.

  2. Open the project in Xojo. The Library file must be present in the folder with the project when the project is opened.

To make a Library available to all projects:

  1. Drag the compiled library file (the file with the .xojo_library file extension) into the Plugins folder.

  2. Open the project with which you wish to use the Library in Xojo. The Library file must be present in the Plugins folder when the project is opened.

While the project items in a Library appear in the Navigator of the source project, when you use a Library, the Library nor any of its items appear in the Navigator. However, they are all available to your project and will autocomplete in the Code Editor. Like a plugin, you will not be able to step into the methods and events of the items in a Library as the code is compiled. The source code is not included in the compiled Library.

Note

Items such as Notes can be included in a Library source project but will not be included in the compiled Library.

Important

Android projects do not yet support using or creating Libraries.

Creating a Library that supports multiple project types

When you build a Library, it only supports the project type of the source project. If you created it in a Desktop project, the compiled Library can only be used by other desktop projects.

However, with a little extra work, you can create a compiled Library file that includes support for more than one type of Xojo project.

To create Library that supports more than one Xojo project type:

  1. Build Library files for each project type you wish to support.

  2. Choose one to be the Library you will distribute.

  3. Change its file extension from .xojo_library to .zip.

  4. Unzip the Library file.

  5. This will create a folder whose name will be the same as your Library.

  6. Inside this folder will be a folder representing the project type. For example, a Library built from a desktop project will include a folder titled, Desktop which will include the compiled code for desktop projects.

  7. For each of your other compiled Libraries that you wish to combine with this one, change the file extension as you did before to .zip and decompress them.

  8. Copy the project type folder from this Library into the Library folder you wish to distribute.

  9. After all project type folders have been copied into the Library folder you wish to distribute, re-zip the folder of the Library you plan to distribute.

  10. Change the file extension from .zip back to .xojo_library.

You now have a Xojo Library that supports multiple project types.

Note

Any classes and modules they share must be included in each project type folder. There is currently no common place to include these.

See also

For additional information, refer to these topics: