Tweaks to build the whole project on Windows with CMake/MSVC 19 in 2026 - #305
Tweaks to build the whole project on Windows with CMake/MSVC 19 in 2026#305twdragon wants to merge 8 commits into
Conversation
twdragon
commented
Jul 30, 2026
- Fixed the PLC port definitions mistakenly left in the BSD section in the test program
- Added CMake tweaks to successfully build the project on Windows natively using git/MSVC/PowerShell
- Implemented TcAdsDll_ROOT CMake variable to let the build system catch the libraries from non-standard locations. Tested on non-native installation in Windows 10.0.19045 and SDK 10.0.26100.0
- Fixed the PLC port definitions mistakenly left in the BSD section
in the test program
- Added CMake tweaks to successfully build the project on Windows natively
using git/MSVC/PowerShell
- Implemented TcAdsDll_ROOT CMake variable to let the build system catch
the libraries from non-standard locations. Tested on non-native
installation in Windows 10.0.19045 and SDK 10.0.26100.0
|
Hello, I am not the maintainer, but I authored the FindTcAdsDll.cmake file that you are editing. I suggest that you should keep the support for finding TcAdsDll in old TwinCAT versions ? The way you are doing it is correct but will only work for the newest versions. The find_library and find_file PATHS argument take an array anyways. Additionally, since the default location is no longer C:/TwinCAT/.. in the new version so we can probably change it to something like this: if (WIN32 AND NOT DEFINED TcAdsDll_ROOT) Try them out, tell me if it works. |
|
@juarezr-mvtec thanks for the suggestion! Unfortunately, direct introduction of array-like path lists did not work, so I used separate variables. |
|
@juarezr-mvtec I will also try to fix the Linux pipeline, but I cannot promise to do it now, so likely it will be a separate PR |
|
Hello @twdragon The problem is that we are passing the variable PATHS with "" like : find_library(TcAdsDll_IMPLIB this makes find_library consider TcAdsDll_IMPLIB_DIR as a single string instead of a list. So removing the "" fixes the issue with passing multiple paths. I tested this on my setup (old version path) and this is working for me: I have not personally tested it but linux should work out of the box at least according to the maintainer (see #295). |
|
@juarezr-mvtec now it looks like: if (WIN32 AND NOT DEFINED TcAdsDll_ROOT)
# Typical install locations on Windows
list(APPEND _TcAdsDll_PATH
"$ENV{SystemDrive}/TwinCAT/AdsApi/TcAdsDll" # Old location (TwinCAT < 3.5)
"$ENV{ProgramFiles(x86)}/Beckhoff/TwinCAT/AdsApi/TcAdsDll" # New location (TwinCAT 3.5+)
)
else ()
set(_TcAdsDll_PATH)
endif ()
# Find the include headers
find_path(TcAdsDll_INCLUDE_DIR
NAMES TcAdsApi.h TcAdsDef.h
PATHS "${_TcAdsDll_PATH_OLD}" "${_TcAdsDll_PATH_NEW}"
PATH_SUFFIXES "Include" "include"
)
# Find all related files base on the include files location. This is done
# assuming that the files are ordered as install by TwinCat. If they are
# in some other configuration this will not work.
if (WIN32)
cmake_path(GET TcAdsDll_INCLUDE_DIR PARENT_PATH TcAdsDll_ROOT_DIR)
if (NOT TcAdsDll_FIND_QUIETLY)
message(STATUS "Searching TcAdsDll in ROOT ${TcAdsDll_ROOT_DIR}")
endif ()
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
set(TcAdsDll_IMPLIB_DIR "${TcAdsDll_ROOT_DIR}/Lib/x64")
list(APPEND TcAdsDll_DLL_DIR
"${TcAdsDll_ROOT_DIR}/x64"
"${TcAdsDll_ROOT_DIR}/../../Common64"
)
elseif (CMAKE_SIZEOF_VOID_P EQUAL 4)
set(TcAdsDll_IMPLIB_DIR "${TcAdsDll_ROOT_DIR}/Lib")
list(APPEND TcAdsDll_DLL_DIR
"${TcAdsDll_ROOT_DIR}"
"${TcAdsDll_ROOT_DIR}/../../Common32"
)
endif ()
find_library(TcAdsDll_IMPLIB
NAMES TcAdsDll.lib
PATHS ${TcAdsDll_IMPLIB_DIR}
NO_DEFAULT_PATH
)
find_file(TcAdsDll_LIBRARY
NAMES TcAdsDll.dll
PATHS ${TcAdsDll_DLL_DIR}
NO_DEFAULT_PATH
)
else ()
find_library(TcAdsDll_LIBRARY
NAMES TcAdsDll
)
endif ()Attested to work on Windows. Thanks again! |
|
@juarezr-mvtec I implemented and tested it, thanks again! |
|
@juarezr-mvtec I unfortunately don't have a different version of TwinCAT libraries on my workstation, so I cannot fully test the setup |
There was a problem hiding this comment.
Some more changes. With the changes this should work as expected.
I download the latest TwinCAT version and I tested this now with and without setting TcAdsDll_ROOT.
Once the changes are complete I will suggest that you SQUASH all of these changes into a single commit to keep a clean git history. :)
@juarezr-mvtec I will do)) I have not so much time to work on this repo, so I do quick and dirty |
|
Thanks a lot to both of you — there is a lot of useful work for CMake in here. Together with Claude I took a look at both of your PRs, and we ended up picking individual commits/changes and rewriting them a bit. Everything we took keeps you credited with a The result is on Everything from here on is AI generated: What we tookFrom @juarezr-mvtec's branch:
From @twdragon's branch:
Four follow-ups came out of reviewing the above, which we added on top: What we changed while taking itThe TwinCAT version numbers. @twdragon, the commit and its comments C4566 / the infinity symbol. We set AdsException. We kept
AMSPORT_R0_PLC_TC3. Same fix, but guarded with README. Reframed, and two fixes to the snippet. We build, test and The snippet itself had two problems. What we did not take, and whyThe So setting
The 32 bit Removing the Removing Once that is fixed the warning can go. Happy to take a patch for it — For next timeThree things that would make patches much quicker to merge:
Thanks again — the Windows side of this project is much better off for |
|
Hello @pbruenn, thanks for taking the changes I will just close my pull request then. Another question, so what is supported to be the cpp standard of the project ? On the previous cmake version it was set to cpp14 so I just keep that (top cmake sets CMAKE_CXX_STANDARD on line 19) . On the readme you mention gcc 10.2 which default to cpp14 as the supported standard so it makes sense to set it to cpp14, but there are also "stable" support of cpp17 so it might make sense to rise it, definitely no support of cpp20 if you want to keep that old toolchain compatibility. Once that is fixed the warning can go. Happy to take a patch for it — I would say that yes, that if AdsLib is compile as a shared library (either linux or windows) it will be nice to export a C API that can be use. That would mean that when loading the StandAlone or TwinCAT version of the AdsLib will work the same from the consumer application perspective. On the StandAlone version gives the full communication protocol while on the TwinCAT version you are basically just proving a thin layer between TcAdsDll in most cases. However, doing so will require that you use different interface, with different function names (e.g., add a prefix AdsLib_X... before C function names) to avoid clashes with what is already exported on TcAdsDll. Additionally, you might have to duplicate certain structures and constant values (e.g., error codes). You will also need an export header of public symbols that is cross compatible with Windows and a better separation of the public and private interfaces of AdsLib. It will be a major refactor and a mayor version break. The benefits is that doing so will stabilize your interfaces (ABI forward compatibility) and make it much easier to make future changes and summit contributions without fear of any small change will break a customers old project. It will also make it much easier for US to push patches to already deploy system by just updating the shared library instead of force recompile of the binary which in this day an age is something that is very important. This is what i was trying to express when i wrote #301. |
|
Hi @juarezr-mvtec, Regarding C++ standards. I have to say I became very conservative regarding c++ standards and will only bump the minimal version if I really need a new feature from more modern standards. Regadring the shared library thing I answered it in your issue to be able to close this PR here if @twdragon agrees we merged everything important from it. |
@pbruenn sure. I think we can close this PR, as you have a selection of changes. I agree. Thanks for merging! |