user7111260
user7111260

Reputation:

C++ macros initialize a variable declared in .h from .cpp

Im trying to load a library based on the operating system this is what I made so far. Although I don't understand why do I get the following error in the .cpp file for the vulkan_lib variable: "this declaration has no storage class or type specifier".

.cpp file:

#include "vulkan_commons.h"

namespace vk {
    #if defined _WIN32
        vulkan_lib = LoadLibrary(L"vulkan-1.dll");
    #elif defined __linux
        vulkan_lib = dlopen("libvulkan.so.1", RTLD_NOW);
    #endif
}

.h file:

#ifndef VULKAN_COMMONS_H
#define VULKAN_COMMONS_H

namespace vk {

#if defined _WIN32
    #include <Windows.h>
    #define LoadFunction GetProcAddress
    #define OS_LIB       HMODULE
#elif defined __linux
    #include <dlfcn.h>
    #define LoadFunction dlsym
    #define OS_LIB       void*
#else
    #error
#endif

OS_LIB vulkan_lib;

}

#endif

Upvotes: 1

Views: 94

Answers (1)

bolov
bolov

Reputation: 75755

in the header:

extern OS_LIB vulkan_lib;

You need extern so that vulkan_lib is not defined in every TU

in the cpp:

OS_LIB vulkan_lib = ...

You need to make it a definition

Upvotes: 2

Related Questions