0

I'm trying to build a static C++ 20 library using CMake 3.25 on Linux Mint with CLion.

The build fails when trying to compile any file which includes the <string> header.

I'm getting a lot of error messages like this one:

In file included from /usr/include/c++/11/bits/localefwd.h:40,
                 from /usr/include/c++/11/string:43,
                 from /home/foo/repos/RMC/SE/src/sstd/misc/output.h:3,
                 from /home/foo/repos/RMC/SE/src/animation/animation_channel.cpp:2:
/usr/include/x86_64-linux-gnu/c++/11/bits/c++locale.h: In function ‘int std::__convert_from_v(__locale_struct* const&, char*, int, const char*, ...)’:
/usr/include/x86_64-linux-gnu/c++/11/bits/c++locale.h:92:48: error: expected primary-expression before ‘,’ token
   92 |     const int __ret = __builtin_vsnprintf(__out, __size, __fmt, __args);
      |                                                ^
In file included from /usr/include/c++/11/string:44,
                 from /home/foo/repos/RMC/SE/src/sstd/misc/output.h:3,
                 from /home/foo/repos/RMC/SE/src/animation/animation_channel.cpp:2:
/usr/include/c++/11/bits/ostream_insert.h: In function ‘void std::__ostream_write(std::basic_ostream<_CharT, _Traits>&, const _CharT*, std::streamsize)’:
/usr/include/c++/11/bits/ostream_insert.h:51:37: error: expected primary-expression before ‘.’ token
   51 |       const streamsize __put = __out.rdbuf()->sputn(__s, __n);
      |                                     ^
/usr/include/c++/11/bits/ostream_insert.h:53:14: error: expected primary-expression before ‘.’ token
   53 |         __out.setstate(__ios_base::badbit);
      |              ^
/usr/include/c++/11/bits/ostream_insert.h: In function ‘void std::__ostream_fill(std::basic_ostream<_CharT, _Traits>&, std::streamsize)’:
/usr/include/c++/11/bits/ostream_insert.h:63:31: error: expected primary-expression before ‘.’ token
   63 |       const _CharT __c = __out.fill();
      |                               ^
/usr/include/c++/11/bits/ostream_insert.h:66:57: error: expected primary-expression before ‘.’ token
   66 |           const typename _Traits::int_type __put = __out.rdbuf()->sputc(__c);
      |                                                         ^
/usr/include/c++/11/bits/ostream_insert.h:69:20: error: expected primary-expression before ‘.’ token
   69 |               __out.setstate(__ios_base::badbit);
      |                    ^

The code builds perfectly fine on Windows using MSBuild. I've tried switching g++ executables, changing the C++ version to 17 and building outside of CLion, with the same result. Here are the files that g++ is complaining about (no errors in CLion):

animation_channel.cpp

#include "animation_channel.h"
#include <misc/output.h>

namespace SE::Animation
{
    Transform AnimationChannel::Sample(const float time) const
    {
        // Check if the parameter is inside the bounds.
        auto lastKeyFrameTime = mKeyTimes[mKeyCount - 1];
        SE_ASSERT(time <= lastKeyFrameTime);

        // Get 2 keys which are the closest to the time; one after and one before it.
        int t;
        for (t = 1; t < mKeyCount; t++)
        {
            if (mKeyTimes[t] > time) break;
        }

        Transform next = mData[t];
        Transform prev = mData[t - 1];
        return Transform::Interpolate(prev, next, time);
    }
}

output.h

#pragma once
#include "../buildsystem.h"
#include <string>
#include <cstdint>

#ifdef SE_DEBUG
#define SE_ASSERT(condition) sstd::Output::Assert(condition, #condition, __FILE__, __LINE__)
#else
#define SE_ASSERT(condition) sstd::Output::Assert(condition, #condition, "unnamed file", __LINE__)
#endif

namespace sstd
{
    enum class MessageType : uint32_t
    {
        Error,
        Warning,
        Info,
        Debug,
        Success,
        ErrorQuit,
    };

    class Output
    {
    public:
        static void WriteLine(const std::string& message, MessageType type);
        static void WriteLine(const std::string& message);
        static void WriteLog(const std::string& message, MessageType type);
        static void Assert(bool condition, const char* conditionString, const char* file, int line);
    };
}

Here's the CMakeLists.txt:

cmake_minimum_required(VERSION 3.25)
project(SE)

set(CMAKE_CXX_STANDARD 20)

include_directories(include)

add_library(SE STATIC 
    ...
)
Ma_rv
  • 223
  • 1
  • 7
  • It looks like the macro `__out` is defined somewhere, perhaps in animation_channel.h or ../buildsystem.h. – 273K May 20 '23 at 22:00
  • My guess would be that you have a `#define __out` somewhere, `__out` is a reserved identifier so shouldn't be used by anything other than the compiler and standard library – Alan Birtles May 20 '23 at 22:01
  • Thanks for the tips! I just checked and building with CMake also works on Windows, but I don't have any occurrence of "__out" or "#define __out" in any of my files. – Ma_rv May 20 '23 at 22:16
  • Nevermind, there is one file "sal.h" inside the include folder with this definition: ``#define __out _SAL1_Source_(__out, (), _Out_)`` – Ma_rv May 20 '23 at 22:22
  • late tip: you can try using the `-E` compile flag to see the preprocessed translation unit (see also https://stackoverflow.com/q/3742822/11107541), and then grep for `__out`. – starball May 20 '23 at 22:23
  • Okay, removing the conflicting directives fixed all those issues, but now ``/usr/include/x86_64-linux-gnu/c++/11/bits/gthr-default.h: In function ‘int __gthread_create(__gthread_t*, void* (*)(void*), void*)’: /.../gthr-default.h:663:52: error: expected primary-expression before ‘,’ token 663 | return __gthrw_(pthread_create) (__threadid, NULL, __func, __args); | ^ /.../gthr-default.h: In function ‘void __gthread_mutex_init_function(__gthread_mutex_t*)’: /.../ :733:48: error: expected primary-expression before ‘)’ token `` – Ma_rv May 20 '23 at 22:35
  • I'm assuming there's another macro somewhere that's messing with this header? – Ma_rv May 20 '23 at 22:38
  • Looks like it's pointing at the `,` just after `NULL` perhaps look for a redefinition of `NULL`. – Richard Critten May 21 '23 at 00:02
  • 1
    Is sal.h the one from the windows sdk? Trying to use Windows headers on another os is a really bad idea and is likely the root of all your problems – Alan Birtles May 21 '23 at 05:55
  • Yes it is, and MS themselves say to use it for DXMath. Adding ``#define PAL_STDCPP_COMPAT`` fixed all errors. – Ma_rv May 21 '23 at 10:56

0 Answers0