Skip to content

Update cmake support for AdsLib#295

Open
izonfreak wants to merge 1 commit into
Beckhoff:masterfrom
izonfreak:update-cmake-support
Open

Update cmake support for AdsLib#295
izonfreak wants to merge 1 commit into
Beckhoff:masterfrom
izonfreak:update-cmake-support

Conversation

@izonfreak

Copy link
Copy Markdown

This commit modernized the cmake implementations and fixes issues when building with the MSVC (Microsoft Visual Compiler). Building with clang and GCC compilers was also tested and supported.
This cmake implementation allows building both variants of the AdsLib (standalone and with twincat rounter) on a single cmake project and is also possible to install both on the same directory (for easy import) in both debug and release builds.

The changes included on this commit are as follows:

  • Increase cmake min version to 3.23
  • Set the current AdsLib version in project() (previously 0.0.20)
  • Add basic module FindTcAdsDll.cmake for locating the TcAdsDll (windows only).
  • Allow building both shared and static library version. Simply set BUILD_SHARED_LIBS to ON or OFF according to your needs. Shared library build with MSVC is not supported. The reason is that the AdsLib does not provide the needed import and export decorators.
  • Add generation of a cmake config file (AdsConfig.cmake) on install and enable using find_package on the consuming project. One only needs to specify the Ads_ROOT variable.
  • Use GNU-style installation directories.
  • Provide library headers under include/ads
  • Improve import target naming.
    • AdsLib : Standalone variant of the AdsLib.
    • TcAdsLib : TwinCAT rounter variant of the AdsLib. Only if TcAdsDll is found and available. Otherwise the target is no created and a warning message is display.
  • Fatal error if BUILD_SHARED_LIBS=ON when using the Microsoft Visual Studio compiler.
  • Allow building tests using cmake.
  • Allow building AdsTool using cmake in both standalone and twincat rounter variants.
  • Add build options:
    • WITH_TEST : When OFF the project does not create/build targets for the different unitests and integration tests.
    • WITH_EXAMPLES : When OFF the project does not create/build targets for the project examples.
    • STANDALONE_ONLY : When ON create/build only the AdsLib stand alone version (does seach for TwinCat TcAdsDll)

Steps for testing/building.

  • Install cmake min v3.23
  • Configure the cmake project with the needed options e.g.:

cmake -G "Ninja"
-B %BUILD_DIR%\release
-DCMAKE_INSTALL_PREFIX=%INSTALL_DIR%
-DCMAKE_BUILD_TYPE=Release
-DCMAKE_PREFIX_PATH=C:\TwinCAT\AdsApi\TcAdsDll
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
-DBUILD_SHARED_LIBS=OFF
-DWITH_TESTS=OFF
-DWITH_EXAMPLES=OFF
-DSTANDALONE_ONLY=OFF

  • Build & install:

cmake --build %BUILD_DIR%\release
cmake --build %BUILD_DIR%\release --target install

@izonfreak

Copy link
Copy Markdown
Author

This pull request should fix open issues like #292 #275 #271 #228

@izonfreak izonfreak force-pushed the update-cmake-support branch from 2262ce5 to ff91c39 Compare June 10, 2026 14:29

@pbruenn pbruenn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @izonfreak ,
thanks for this PR. I can't really review the CMake changes, as they are more of a community effort. I chose Meson over CMake because, at the time, it seemed much cleaner to me (and I still think so today).
That said, the reality is that CMake is used in many more places, and I understand the importance of supporting it. Therefore, I'm happy to merge the CMake--related changes as they are, provided that my small CMake test continues to pass:

ADS/.gitlab-ci.yml

Lines 113 to 114 in 6788651

- cmake -S . -B build
- cmake --build build

However, please split the other minor changes into separate commits, and then we can merge this PR, thanks.

p.s.: I just updated the CONTRIBUTING file to make clear we prefer logical split commits.

Comment thread AdsLib/MasterDcStatAccess.cpp Outdated
rowNames{ {
"1", "2", "5", "10", "20", "50", "100", "200", "500",
"\u221E" // infinity symbol
u8"\u221E" // infinity symbol

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep this separate from the CMake changes and put it into its own commit explaining why this is needed.

Comment thread AdsLib/Sockets.cpp Outdated
}
if (SOCK_STREAM == type) {
if (::connect(m_Socket, rp->ai_addr, rp->ai_addrlen)) {
if (::connect(m_Socket, rp->ai_addr, static_cast<int>(rp->ai_addrlen))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, please separate this from the CMake changes and put it into its own commit and explain why we need the change.

Comment thread .gitignore Outdated
.vscode/

# Clion
.idea/

@pbruenn pbruenn Jun 13, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I know this file is already quite heavy and I already forgot forgotten about it, if until you didn't remember until you reminded me.
I don't want to bloat it even more with IDE specific stuff.
Git supports $XDG_CONFIG_HOME/git/ignore which is much better suited for developer specific IDE files. You won't even need to copy & paste your .gitignore settings into every project anymore.
https://git-scm.com/docs/gitignore

@izonfreak

izonfreak commented Jun 15, 2026

Copy link
Copy Markdown
Author

Hi @izonfreak , thanks for this PR. I can't really review the CMake changes, as they are more of a community effort. I chose Meson over CMake because, at the time, it seemed much cleaner to me (and I still think so today). That said, the reality is that CMake is used in many more places, and I understand the importance of supporting it. Therefore, I'm happy to merge the CMake--related changes as they are, provided that my small CMake test continues to pass:

ADS/.gitlab-ci.yml

Lines 113 to 114 in 6788651

- cmake -S . -B build
- cmake --build build

However, please split the other minor changes into separate commits, and then we can merge this PR, thanks.

p.s.: I just updated the CONTRIBUTING file to make clear we prefer logical split commits.

Hello @pbruenn,

I will move the other small changes to their own pull request, but they are mostly fixes so that the sources compile without warnings in the different toolchains i am testing with. Before we merge I do have some questions mostly about lib names because i saw it on another issue.

  1. Is there a linux equivalent to the TcAdsDll on Unix ? if so, what is the name ( is there a SONAME) and what is usual the install location on the system? The FindTcAdsDll.cmake module find the library on windows and i would like to add also some kind of linux support if it exist even if i cant directly tested right away. (I have no Beckhoff device with Linux or FreeBSD)
  2. What are the expected name(s) of compiled binaries? My pull request will generate two binaries as:
    • Standalone shared lib it will be name libAdsLib.so on linux and AdsLib.dll ( if we could) on windows
    • With TwinCat router shared lib (if exists in point 1) will be name libTcAdsLib.so and TcAdsLib.dll ( if we could) on windows.
    • AdsTool will be AdsTool.exe (on Windows) or AdsTool (on Linux)
    • AdsTool with TwinCat router will be TcAdsTool.exe (on Windows) or TcAdsTool (on Linux)
  3. Is there a SONAME for libAdsLib.so ? If so where can i find it ?
  4. Where do you usually set the version number of the library ? e.g., 113.X.Y. Right now I have the value hardcoded in the cmake file. To avoid that the cmake becomes out of sync with the project I can also make the cmake script read the version from a default location that we agree on (e.g., include header or meson file)

Are my name assumptions correct ? The reason for the different names is so that they can all be install together in the same location like the image bellow. The ones with (d) are the debug version.

image

cmake can find the right version and file base on the consuming project configuration:

image

Ands is use like:


# Find TcAdsDll with FindTcAdsDll.cmake module
find_package(TcAdsDll MODULE REQUIRED GLOBAL)
# Find the AdsLib itself
find_package(Ads CONFIG REQUIRED AdsLib TcAdsLib GLOBAL)

# Link against stand alone
add_executable(StandAlone)
target_link_libraries(StandAlone PRIVATE Ads::AdsLib)

# Link against with twincat router. Requires linking against TcAdsDll as well. 
add_executable(WithRouter)
target_link_libraries(WithRouter PRIVATE TcAdsDll::TcAdsDll Ads::TcAdsLib)

@pbruenn

pbruenn commented Jun 15, 2026

Copy link
Copy Markdown
Member

Hi @izonfreak,
on a Linux system with TwinCAT Runtime and Router installed (tc31-xar-um) you should have a package named "libadscomm" installed, too. That one installs the shared library equivalent to TcAdsDll on Windows:

Administrator@PC-8CD98F:~$ dpkg --listfiles libadscomm | grep TcAdsDll
/usr/lib/libTcAdsDll.so

On TC/BSD it's:

Administrator@PC-8CD98F:~ $ pkg which /usr/local/lib/libTcAdsDll.so 
/usr/local/lib/libTcAdsDll.so was installed by package TF6000-ADS-Comm-Lib-4026.22.53

On Linux and FreeBSD our binaries are called:

  • adstool
  • libAdsLib.a
  • libAdsLib.so
  • libTcAdsLib.a
  • tcadstool

We don't set SONAME at the moment, neither on Linux or FreeBSD:

Administrator@PC-8CD98F:~/ads $ readelf -d build/libAdsLib.so | grep SONAME
 0x000000000000000e SONAME               Library soname: [libAdsLib.so]

At the moment I increment the version manually in two places debian/changelog and AdsTool/main.cpp see 6788651

Normally, debian/changelog is the source of our versions, so I suggest you use dpkg-parsechangelog --show-field Version or awk -F'[()]' '{print $2; exit}' debian/changelog

If you have access to mybeckhoff and can download "Beckhoff RT Linux" you can install it into a VM. KVM works fine and Hyper-V should, too.

This commit modernized the cmake implementations and fixes issues when
building with the MSVC (Microsoft Visual Compiler). Building with clang
and GCC compilers was also tested and supported.
This cmake implementation allows building both variants of
the AdsLib (standalone and with twincat rounter) on a single cmake
project and is also possible to install both on the same directory (for
easy import) in both debug and release builds.

The changes included on this commit are as follows:

- Increase cmake min version to 3.23
- Set the current AdsLib version in project(). Read directly from
  debian/changelog file.
- Add basic module FindTcAdsDll.cmake for locating the TcAdsDll (windows
  only). Linux support needed.
- Allow building both shared and static library version. Simply set
  BUILD_SHARED_LIBS to ON or OFF according to your needs.
- Add generation of a cmake config file (AdsConfig.cmake) on install and
  enebale using find_package on the consuming project. One only needs to
  specify the Ads_ROOT variable.
- Use GNU-style installation directories.
- Provide library headers under include/ads
- Improve import target naming.
   - AdsLib : Standalone variant of the AdsLib.
   - TcAdsLib : TwinCAT rounter variant of the AdsLib. Only if TcAdsDll
     is found and available. Otherwise the target is no created and a
     warning message is display.
- Fatal error if BUILD_SHARED_LIBS=ON when using the microsoft visual
  studio compiler. The reson is that the AdsLib does not provide the
  needed import and export decorators.
- Allow building tests using cmake.
- Allow building AdsTool using cmake in both standalone and twincat
  rounter variants.
- Add build options:
   - WITH_TEST : When OFF the project does not create/build targets for the
     different unitests and integration tests.
   - WITH_EXAMPLES : When OFF the project does not create/build targets
     for the project examples.
   - STANDALONE_ONLY : When ON create/build only the AdsLib stand alone
     version (does seach for TwinCat TcAdsDll)

Steps for testing/building.

 - Install cmake min v3.23
 - Configure the cmake project with the needed options:

 cmake -G "Ninja"
      -B %BUILD_DIR%\release
      -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR%
      -DCMAKE_BUILD_TYPE=Release
      -DCMAKE_PREFIX_PATH=C:\TwinCAT\AdsApi\TcAdsDll
      -DCMAKE_POSITION_INDEPENDENT_CODE=ON
      -DBUILD_SHARED_LIBS=OFF
      -DWITH_TESTS=OFF
      -DWITH_EXAMPLES=OFF
      -DSTANDALONE_ONLY=OFF

 - Build & install:

  cmake --build %BUILD_DIR%\release
  cmake --build %BUILD_DIR%\release --target install
@izonfreak izonfreak force-pushed the update-cmake-support branch from ff91c39 to 61b27b0 Compare June 16, 2026 12:21
@izonfreak

Copy link
Copy Markdown
Author

Hello @pbruenn, the requested changes have been implemented. The only thing that i omitted is adding support for finding libTcAdsDll.so until I can download and test the twincat runtime on linux and make sure that it works. I might have time for this later this year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants