Environment: Windows 10 with CMake 3.19.4 + MinGW 7.3.0
When building libraries with CMake + MinGW that contain tests or examples (.exe files) that consume the compiled library, then the compilation of these executables causes errors like this:
error: function 'ur_rtde::RTDE::RobotCommand::RobotCommand()' definition is marked dllimport
This happens for example for this library: https://gitlab.com/sdurobotics/ur_rtde
During the CMake configuration step, an export file is generated by CMake: rtde_export.h
#ifndef RTDE_EXPORT_H
#define RTDE_EXPORT_H
#ifdef RTDE_STATIC_DEFINE
# define RTDE_EXPORT
# define RTDE_NO_EXPORT
#else
# ifndef RTDE_EXPORT
# ifdef rtde_EXPORTS
/* We are building this library */
# define RTDE_EXPORT __declspec(dllexport)
# else
/* We are using this library */
# define RTDE_EXPORT __declspec(dllimport)
# endif
# endif
# ifndef RTDE_NO_EXPORT
# define RTDE_NO_EXPORT
# endif
#endif
#ifndef RTDE_DEPRECATED
# define RTDE_DEPRECATED __attribute__ ((__deprecated__))
#endif
#ifndef RTDE_DEPRECATED_EXPORT
# define RTDE_DEPRECATED_EXPORT RTDE_EXPORT RTDE_DEPRECATED
#endif
#ifndef RTDE_DEPRECATED_NO_EXPORT
# define RTDE_DEPRECATED_NO_EXPORT RTDE_NO_EXPORT RTDE_DEPRECATED
#endif
#if 0 /* DEFINE_NO_DEPRECATED */
# ifndef RTDE_NO_DEPRECATED
# define RTDE_NO_DEPRECATED
# endif
#endif
#endif /* RTDE_EXPORT_H */
This file generates the dllexport / dllimport keywords. While the compilation does not produce any errors when compiling with MSVC compiler, it fails when compiling with MinGW compiler. Normally the dllexport / dllimport definitions are not required for MinGW because it can link directly to DLLs and an import library is not required. I can fix the build error by manually editing the generated export file and defining an empty export macro like in line 4 in the code below.
#ifndef RTDE_EXPORT_H
#define RTDE_EXPORT_H
#define RTDE_EXPORT // empty export macro to fix compilation error
#ifdef RTDE_STATIC_DEFINE
# define RTDE_EXPORT
# define RTDE_NO_EXPORT
#else
# ifndef RTDE_EXPORT
# ifdef rtde_EXPORTS
This change fixes the build error and enables the complete build of the library but I don’t think that this is the right way to fix this.
So my question is, how should I properly fix the build issue when building a project witg CMake + MinGW ? Is there some setting in CMake that I can change during configuration to fix this and why does CMake generate dllexport / dllimport statements for MinGW if this is not required for MinGW?
Thank you for any help,
Uwe