Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c++11 compile error in Mac OS X #302

Closed
firatyenidunya opened this issue Jan 13, 2018 · 1 comment
Closed

c++11 compile error in Mac OS X #302

firatyenidunya opened this issue Jan 13, 2018 · 1 comment

Comments

@firatyenidunya
Copy link

I have been developing a C++ OpenGL game project. CMake is used as the build system. I'm using nanogui in the project as the external GUI library.

I was using glfw and glad outside of the nanogui and I managed to link them externally to the nanogui's CMake.

  • I can successfully build the project on Linux.
  • I could successfully build the library by itself from an another directory with the glfw and glad in it.
  • But when I'm trying to build the same project on macOS Sierra, I get the following Make errors.

Everything successfully built and until up here

In file included from /Users/firat/Desktop/dungeon-defense/main.cpp:11:
In file included from /Users/firat/Desktop/dungeon-defense/include/GUI.h:5:
In file included from /Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/nanogui.h:14:
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:124:6: warning: scoped enumerations are a C++11 extension [-Wc++11-extensions]
enum class Cursor {
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:136:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Vector2f = Eigen::Vector2f;
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:138:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Vector3f = Eigen::Vector3f;
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:140:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Vector4f = Eigen::Vector4f;
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:142:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Vector2i = Eigen::Vector2i;
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:144:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Vector3i = Eigen::Vector3i;
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:146:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Vector4i = Eigen::Vector4i;
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:148:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Matrix3f = Eigen::Matrix3f;
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:150:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using Matrix4f = Eigen::Matrix4f;
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:152:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using VectorXf = Eigen::VectorXf;
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:154:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using MatrixXf = Eigen::MatrixXf;
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:160:18: warning: alias declarations are a C++11 extension [-Wc++11-extensions]
using MatrixXu = Eigen::Matrix<uint32_t, Eigen::Dynamic, Eigen::Dynamic>;
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:195:15: error: delegating constructors are permitted only in C++11
Color() : Color(0, 0, 0, 0) {}
^~~~~
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:216:11: error: delegating constructors are permitted only in C++11
: Color(color(0), color(1), color(2), alpha) { }
^~~~~
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:231:11: error: delegating constructors are permitted only in C++11
: Color(color.cast() / 255.f, alpha / 255.f) { }
^~~~~
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:240:43: error: delegating constructors are permitted only in C++11
Color(const Eigen::Vector3f &color) : Color(color, 1.0f) {}
^~~~~
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:250:11: error: delegating constructors are permitted only in C++11
: Color((Vector3f)(color.cast() / 255.f)) { }
^~~~~
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:260:11: error: delegating constructors are permitted only in C++11
: Color((Vector4f)(color.cast() / 255.f)) { }
^~~~~
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:272:11: error: delegating constructors are permitted only in C++11
: Color(Vector3f::Constant(intensity), alpha) { }
^~~~~
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:285:11: error: delegating constructors are permitted only in C++11
: Color(Vector3i::Constant(intensity), alpha) { }
^~~~~
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:302:49: error: delegating constructors are permitted only in C++11
Color(float r, float g, float b, float a) : Color(Vector4f(r, g, b, a)) { }
^~~~~
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:320:41: error: delegating constructors are permitted only in C++11
Color(int r, int g, int b, int a) : Color(Vector4i(r, g, b, a)) { }
^~~~~
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:475:65: error: a space is required between consecutive right angle brackets (use '> >')
file_dialog(const std::vector<std::pair<std::string, std::string>> &filetypes,
^~
> >
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:494:65: error: a space is required between consecutive right angle brackets (use '> >')
file_dialog(const std::vector<std::pair<std::string, std::string>> &filetypes,
^~
> >
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/common.h:521:61: error: a space is required between consecutive right angle brackets (use '> >')
extern NANOGUI_EXPORT std::vector<std::pair<int, std::string>>
^~
> >
In file included from /Users/firat/Desktop/dungeon-defense/main.cpp:11:
In file included from /Users/firat/Desktop/dungeon-defense/include/GUI.h:5:
In file included from /Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/nanogui.h:15:
In file included from /Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/widget.h:15:
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/object.h:45:43: error: expected ';' at end of declaration list
void decRef(bool dealloc = true) const noexcept;
^
;
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/object.h:52:30: error: function definition does not declare parameters
mutable std::atomic m_refCount { 0 };
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/object.h:31:30: error: member initializer 'm_refCount' does not name a non-static data member or base class
Object(const Object &) : m_refCount(0) {}
^~~~~~~~~~~~~
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/object.h:34:38: error: use of undeclared identifier 'm_refCount'; did you mean 'getRefCount'?
int getRefCount() const { return m_refCount; };
^~~~~~~~~~
getRefCount
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/object.h:34:9: note: 'getRefCount' declared here
int getRefCount() const { return m_refCount; };
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/object.h:37:29: error: use of undeclared identifier 'm_refCount'
void incRef() const { ++m_refCount; }
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/object.h:87:13: warning: rvalue references are a C++11 extension [-Wc++11-extensions]
ref(ref &&r) noexcept : m_ptr(r.m_ptr) {
^
/Users/firat/Desktop/dungeon-defense/external/nanogui/include/nanogui/object.h:87:17: error: expected ';' at end of declaration list
ref(ref &&r) noexcept : m_ptr(r.m_ptr) {
^
;
fatal error: too many errors emitted, stopping now [-ferror-limit=]
13 warnings and 20 errors generated.
make[3]: *** [CMakeFiles/dungeon-defense.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/dungeon-defense.dir/all] Error 2
make[1]: *** [CMakeFiles/dungeon-defense.dir/rule] Error 2
make: *** [dungeon-defense] Error 2

From what I can understand, it has something to do with the compiler not being c++11.

  • I checked that I do have clang with c++11 support when I built some side projects with it.
  • I checked that nothing is wrong with nanogui's CMakeFiles.txt that keeps it from using c++11
  • I recently added following lines to the main CMakeFiles.txt in case it overrides? (I don't know a lot about CMake as you can see)
    if (POLICY CMP0058)
    cmake_policy(SET CMP0058 NEW)
    endif()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")

What do you think about it, what could be the problem? If you need more information let me know!

@svenevs
Copy link
Collaborator

svenevs commented Jan 14, 2018

Hi @firatyenidunya, in order to use NanoGUI, you need more than just a c++11 capable compiler -- you have to compile with c++11 (or higher).

I checked that nothing is wrong with nanogui's CMakeFiles.txt that keeps it from using c++11

It goes the other way, you have to tell CMake which standard to use. The most platform-agnostic way to do this is at the beginning:

cmake_minimum_required(VERSION 3.1.3)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)  # not necessary, but encouraged

The 3.1.3 minimum version is important, as that's when CMAKE_CXX_STANDARD was introduced (as far as I can tell). What the second line does is inform CMake to use the right C++11 flag for the current compiler. With clang, this will typically be -std=c++11, but with GCC it may be e.g. -std=gnu++11. The flag depends on the compiler.

So the compiler errors you are seeing are almost certainly because you need to compile your library with C++11.

NOTE: I advise doing it at the beginning because this has to be done before your CMake "target" is created. If the target is created before you set CMAKE_CXX_STANDARD, it will not apply to that target. So for example

# DOES work
set(CMAKE_CXX_STANDARD 11)
add_executable(myExe main.cpp) # compiled with c++11

# does NOT work
add_executable(myExe main.cpp)
set(CMAKE_CXX_STANDARD 11) # does not apply to `myExe`

I recently added following lines to the main CMakeFiles.txt in case it overrides? (I don't know a lot about CMake as you can see)

if (POLICY CMP0058)
  cmake_policy(SET CMP0058 NEW)
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")

This is independent of the compiler errors you have. In C++ there's something called "ABI", where libc++ comes in is with clang specifically. NanoGUI by default uses libc++ when clang is the compiler, but this shouldn't cause you problems on OSX (if anything, it would be a problem on Linux). Sparing the gory details, if you really want to make sure libc++ is always used with your project, you could mirror what NanoGUI does (with proper citation of course 😉):

  1. The following macro is used, I think you can use it in your own project after doing add_subdirectory(nanogui):

    nanogui/CMakeLists.txt

    Lines 53 to 60 in 3e0b2c8

    macro(CHECK_CXX_COMPILER_AND_LINKER_FLAGS _RESULT _CXX_FLAGS _LINKER_FLAGS)
    set(CMAKE_REQUIRED_FLAGS ${_CXX_FLAGS})
    set(CMAKE_REQUIRED_LIBRARIES ${_LINKER_FLAGS})
    set(CMAKE_REQUIRED_QUIET TRUE)
    check_cxx_source_runs("int main(int argc, char **argv) { return 0; }" ${_RESULT})
    set(CMAKE_REQUIRED_FLAGS "")
    set(CMAKE_REQUIRED_LIBRARIES "")
    endmacro()

  2. Further down, when clang is the compiler:

    nanogui/CMakeLists.txt

    Lines 174 to 192 in 3e0b2c8

    # Always use libc++ on Clang
    if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    CHECK_CXX_COMPILER_AND_LINKER_FLAGS(HAS_LIBCPP "-stdlib=libc++" "-stdlib=libc++")
    if (HAS_LIBCPP)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -stdlib=libc++")
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -stdlib=libc++")
    CHECK_CXX_COMPILER_AND_LINKER_FLAGS(HAS_LIBCPPABI "-stdlib=libc++" "-stdlib=libc++ -lc++abi")
    if(HAS_LIBCPPABI)
    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lc++abi")
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lc++abi")
    message(STATUS "NanoGUI: using libc++ and libc++abi.")
    else()
    message(STATUS "NanoGUI: using libc++.")
    endif()
    else()
    message(STATUS "NanoGUI: NOT using libc++.")
    endif()
    endif()

This would always get triggered for you on OSX unless you are using a non-default compiler.

I hope that helps get you in the right direction :)

@svenevs svenevs closed this as completed May 19, 2019
srenatus added a commit to srenatus/wasm-tools-1 that referenced this issue Jul 1, 2022
This was needed to have this compile on my macos machine.

Before, I ran into errors like

    "error: delegating constructors are permitted only in C++11"

Found the fix searching for that in this issue:
wjakob/nanogui#302

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
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

No branches or pull requests

2 participants