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

Failing to link Magnum::Math? #609

Closed
Reticulatas opened this issue Nov 28, 2022 · 3 comments
Closed

Failing to link Magnum::Math? #609

Reticulatas opened this issue Nov 28, 2022 · 3 comments

Comments

@Reticulatas
Copy link

Reticulatas commented Nov 28, 2022

This might be a misunderstanding of CMake on my part. Compiling a simple project is giving me

F:\Projects\Forlorn\src\game\Actor.h(16,10): error C2079: 'Actor::Position' uses undefined class 'Magnum::Math::Vector3
<Magnum::Float>' [F:\Projects\Forlorn\build\src\Forlorn.vcxproj]

After following the setup guide, I added a new file to the bootstrap:

#pragma once

#include <Magnum/Magnum.h>
#include <Magnum/Types.h>
#include <Magnum/Math/Math.h>

using namespace Magnum;

class Actor
{
private:
	Vector3 Position;
};

Here is my CMakeLists:

project(Forlorn CXX)

# Add module path in case this is project root
if(PROJECT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
    set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/../../modules/" ${CMAKE_MODULE_PATH})
endif()

find_package(Corrade REQUIRED Main)
find_package(Magnum REQUIRED 
	GL 
	Shaders
	Sdl2Application
)

set_directory_properties(PROPERTIES CORRADE_USE_PEDANTIC_FLAGS ON)

add_executable(Forlorn WIN32
	MyApplication.cpp
	game/Actor.cpp
	game/Actor.h
)

target_include_directories(Forlorn PRIVATE
	${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}
)

target_link_libraries(Forlorn PRIVATE
	Corrade::Main
    Magnum::Application
    Magnum::GL
    Magnum::Magnum
	Magnum::Shaders
	Magnum::Primitives
	Magnum::SceneGraph
)

install(TARGETS Forlorn DESTINATION ${MAGNUM_BINARY_INSTALL_DIR})

# Make the executable a default target to build & run in Visual Studio
set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_START

Building using VS0222 after running

cmake -DCMAKE_BUILD_TYPE=Debug ..

I can see the magnum lib being included in VS and there are no other errors- What is happening here? Tried Clean/Rebuild, remake CMake.

@Reticulatas
Copy link
Author

I figured this out. I'm not sure why, but the build succeeds when I do:
#include <Magnum/Platform/Sdl2Application.h>
instead of
#include <Magnum/Magnum.h>

@mosra
Copy link
Owner

mosra commented Nov 29, 2022

Hi!

It's not because of CMake, it's because most Magnum types are forward-declared unless you explicitly include the header the type is defined in. Which is the case of <Magnum/Magnum.h> that contains mainly just forward declarations of essential types, and to get the definition of Vector3 you have to #include <Magnum/Math/Vector3.h>. To make it easier for you, each API in the docs explicitly lists what header to include to get the full definition.

It worked with <Magnum/Platform/Sdl2Application.h> only because that header actually needs the full definition of the vector and so it includes <Magnum/Math/Vector3.h> on its own.

I admit that this might feel unusual at first, as <SomeLibrary/SomeLibrary.h> usually pulls in all types defined by given library. (For example Bullet, Eigen or Unreal does exactly that.) The reason for Magnum having just forward declarations in those headers is to optimize compilation times, see this part of the documentation for more information.

Hope this helps! :)

@Reticulatas
Copy link
Author

Thanks for the explanation, makes sense upon reading that doc!

Maybe a footnote in the example docs about includes linking to that page would be good?

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