Skip to content

Commit 9e9405c

Browse files
committed
Add VOLK_NAMESPACE configuration to use C++ namespaces
In some cases the use of global vk* symbols is inconvenient, as it can conflict with vk* functions defined by the loader; generally speaking this should not be required, but some dynamic library cases are difficult to support cleanly. This problem can be solved when C++ is available: by building volk.h/c as C++ and wrapping functions in volk namespace, which is automatically used, the compiler can still find the functions so no change in source code is necessary, but the global symbols get C++ mangling that never overlaps with vk*. This is not designed as a main mode of operation, but it can be useful when the default setup doesn't fully work.
1 parent 0b17a76 commit 9e9405c

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

CMakeLists.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required(VERSION 3.5...3.30)
22

3-
project(volk VERSION
3+
project(volk VERSION
44
# VOLK_GENERATE_VERSION
55
304
66
# VOLK_GENERATE_VERSION
@@ -18,6 +18,9 @@ endif()
1818
if(NOT DEFINED VOLK_INSTALL)
1919
option(VOLK_INSTALL "Create installation targets" OFF)
2020
endif()
21+
if(NOT DEFINED VOLK_NAMESPACE)
22+
option(VOLK_NAMESPACE "Use C++ namespace for vk* functions" OFF)
23+
endif()
2124
if(NOT DEFINED VOLK_HEADERS_ONLY)
2225
option(VOLK_HEADERS_ONLY "Add interface library only" OFF)
2326
endif()
@@ -35,6 +38,10 @@ if(NOT VOLK_HEADERS_ONLY OR VOLK_INSTALL)
3538
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>
3639
$<INSTALL_INTERFACE:include>
3740
)
41+
if(VOLK_NAMESPACE)
42+
target_compile_definitions(volk PUBLIC VOLK_NAMESPACE)
43+
set_source_files_properties(volk.c PROPERTIES LANGUAGE CXX)
44+
endif()
3845
if(VOLK_STATIC_DEFINES)
3946
target_compile_definitions(volk PUBLIC ${VOLK_STATIC_DEFINES})
4047
endif()

volk.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@
2222
#endif
2323

2424
#ifdef __cplusplus
25+
#ifdef VOLK_NAMESPACE
26+
namespace volk {
27+
#else
2528
extern "C" {
2629
#endif
30+
#endif
2731

2832
#ifdef _WIN32
2933
__declspec(dllimport) HMODULE __stdcall LoadLibraryA(LPCSTR);
@@ -3360,6 +3364,6 @@ PFN_vkAcquireNextImage2KHR vkAcquireNextImage2KHR;
33603364
#endif
33613365

33623366
#ifdef __cplusplus
3363-
}
3367+
} // extern "C" / namespace volk
33643368
#endif
33653369
/* clang-format on */

volk.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#ifndef VOLK_H_
1111
#define VOLK_H_
1212

13+
#if defined(VOLK_NAMESPACE) && !defined(__cplusplus)
14+
#error VOLK_NAMESPACE is only supported in C++
15+
#endif
16+
1317
#if defined(VULKAN_H_) && !defined(VK_NO_PROTOTYPES)
1418
# error To use volk, you need to define VK_NO_PROTOTYPES before including vulkan.h
1519
#endif
@@ -52,8 +56,12 @@
5256
#endif
5357

5458
#ifdef __cplusplus
59+
#ifdef VOLK_NAMESPACE
60+
namespace volk {
61+
#else
5562
extern "C" {
5663
#endif
64+
#endif
5765

5866
struct VolkDeviceTable;
5967

@@ -2140,11 +2148,15 @@ extern PFN_vkAcquireNextImage2KHR vkAcquireNextImage2KHR;
21402148
/* VOLK_GENERATE_PROTOTYPES_H */
21412149

21422150
#ifdef __cplusplus
2143-
}
2151+
} // extern "C" / namespace volk
21442152
#endif
21452153

2154+
#ifdef VOLK_NAMESPACE
2155+
using namespace volk;
21462156
#endif
21472157

2158+
#endif // VOLK_H
2159+
21482160
#ifdef VOLK_IMPLEMENTATION
21492161
#undef VOLK_IMPLEMENTATION
21502162
/* Prevent tools like dependency checkers from detecting a cyclic dependency */

0 commit comments

Comments
 (0)