I have to port some code from Microsoft Visual Studio Compiler to clang. Which pulled some nerf of me. Because I have to make sure, that the code still is compileable/linkable with MVSC. Following code is the corpus delicti. Important is, I cannot split the code into src and header file.
Compiler: 6.0.0-1ubuntu2 and Visual Studio 2015
C++: version 14
OS: Ubuntu 18.04 and Windows 10/7
The code is in a header-file. I include it in several src-files.
CODE:
#ifndef GLOBAL_SETTINGS_
#define GLOBAL_SETTINGS_
#include <cstdint>
namespace global {
enum resolution {
Hz,
kHz,
MHz
};
template<resolution T>
struct sys_clk
{
static const double frequency;
};
#define SYS_CLK_FREQ (115.0e6)
template<> const double sys_clk<Hz>::frequency = SYS_CLK_FREQ;
template<> const double sys_clk<kHz>::frequency = SYS_CLK_FREQ/1.0e3;
template<> const double sys_clk<MHz>::frequency = SYS_CLK_FREQ/1.0e6;
#undef SYS_CLK_FREQ
} // namespace global
#endif /* GLOBAL_SETTINGS_ */
This compiles just fine, but the linker finds himself an unsolvable situation.
LINKER ERROR:
multiple definition of `global::sys_clk_scon<(global::resolution)0>::frequency'
multiple definition of `global::sys_clk_scon<(global::resolution)1>::frequency'
multiple definition of `global::sys_clk_scon<(global::resolution)2>::frequency'
QUESTION:
How is this linker error solvable, so it compiles in MVSC and clang?
.cppfiles this is included in the "primary" one. In there,#define XYZor similar before includingGlobalSettings.hand then wrap the explicit instantiations in#ifdef XYZ. But this is just a terrible way to emulate using a dedicated.cppfile. Can you elaborate why this is not actually an option?frequencyinline. clang should support, you might compile conditionally (#if __cplusplus > [...]).#if __cplusplus > [...] inline # endif static const ...?