Skip to content

Commit 27db090

Browse files
authored
Merge pull request #61 from cginternals/clang_visibility_fix
Add explicit visibility for class and function templates (closes #56)
2 parents ffb1664 + f902132 commit 27db090

File tree

8 files changed

+135
-7
lines changed

8 files changed

+135
-7
lines changed

cmake/Custom.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,16 @@ function(list_extract OUTPUT REGEX)
4444
set(${OUTPUT} ${${OUTPUT}} PARENT_SCOPE)
4545

4646
endfunction(list_extract)
47+
48+
49+
# Creates an export header similar to generate_export_header, but for templates.
50+
# The main difference is that for MSVC, templates must not get exported.
51+
# When the file ${export_file} is included in source code, the macro ${target_id}_TEMPLATE_API
52+
# may get used to define public visibility for templates on GCC and Clang platforms.
53+
function(generate_template_export_header target target_id export_file)
54+
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC")
55+
configure_file(${PROJECT_SOURCE_DIR}/source/codegeneration/template_msvc_api.h.in ${CMAKE_CURRENT_BINARY_DIR}/${export_file})
56+
else()
57+
configure_file(${PROJECT_SOURCE_DIR}/source/codegeneration/template_api.h.in ${CMAKE_CURRENT_BINARY_DIR}/${export_file})
58+
endif()
59+
endfunction()

source/baselib/CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ message(STATUS "Lib ${target}")
1919
# Set API export file and macro
2020
string(MAKE_C_IDENTIFIER ${target} target_id)
2121
string(TOUPPER ${target_id} target_id)
22-
set(feature_file "include/${target}/${target}_features.h")
23-
set(export_file "include/${target}/${target}_api.h")
24-
set(export_macro "${target_id}_API")
22+
set(feature_file "include/${target}/${target}_features.h")
23+
set(export_file "include/${target}/${target}_export.h")
24+
set(template_export_file "include/${target}/${target}_api.h")
25+
set(export_macro "${target_id}_API")
2526

2627

2728
#
@@ -90,6 +91,10 @@ generate_export_header(${target}
9091
EXPORT_FILE_NAME ${export_file}
9192
EXPORT_MACRO_NAME ${export_macro}
9293
)
94+
generate_template_export_header(${target}
95+
${target_id}
96+
${template_export_file}
97+
)
9398

9499

95100
#
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
#ifndef ${target_id}_TEMPLATE_API_H
3+
#define ${target_id}_TEMPLATE_API_H
4+
5+
#include <${target}/${target}_export.h>
6+
7+
#ifdef ${target_id}_STATIC_DEFINE
8+
# define ${target_id}_TEMPLATE_API
9+
#else
10+
# ifndef ${target_id}_TEMPLATE_API
11+
# ifdef ${target}_EXPORTS
12+
/* We are building this library */
13+
# define ${target_id}_TEMPLATE_API __attribute__((visibility("default")))
14+
# else
15+
/* We are using this library */
16+
# define ${target_id}_TEMPLATE_API __attribute__((visibility("default")))
17+
# endif
18+
# endif
19+
20+
#endif
21+
22+
#endif
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
#ifndef ${target_id}_TEMPLATE_API_H
3+
#define ${target_id}_TEMPLATE_API_H
4+
5+
#include <${target}/${target}_export.h>
6+
7+
#ifdef ${target_id}_STATIC_DEFINE
8+
# define ${target_id}_TEMPLATE_API
9+
#else
10+
# ifndef ${target_id}_TEMPLATE_API
11+
# ifdef ${target}_EXPORTS
12+
/* We are building this library */
13+
# define ${target_id}_TEMPLATE_API
14+
# else
15+
/* We are using this library */
16+
# define ${target_id}_TEMPLATE_API
17+
# endif
18+
# endif
19+
20+
#endif
21+
22+
#endif

source/examples/fibcmd/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <baselib/baselib.h>
55

6+
#include <fiblib/CTFibonacci.h>
67
#include <fiblib/Fibonacci.h>
78

89

@@ -15,7 +16,8 @@ int main(int /*argc*/, char* /*argv*/[])
1516
// Calculate and print fibonacci number
1617
std::cout << "Fibonacci library" << std::endl;
1718
std::cout << "========================================" << std::endl;
18-
std::cout << "Fibonacci(8) = " << fiblib::Fibonacci()(8) << std::endl;
19+
std::cout << "CTFibonacci(6) = " << fiblib::CTFibonacci<6>::value << std::endl;
20+
std::cout << "Fibonacci(8) = " << fiblib::Fibonacci()(8) << std::endl;
1921
std::cout << std::endl;
2022

2123
return 0;

source/fiblib/CMakeLists.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ message(STATUS "Lib ${target}")
1919
# Set API export file and macro
2020
string(MAKE_C_IDENTIFIER ${target} target_id)
2121
string(TOUPPER ${target_id} target_id)
22-
set(feature_file "include/${target}/${target}_features.h")
23-
set(export_file "include/${target}/${target}_api.h")
24-
set(export_macro "${target_id}_API")
22+
set(feature_file "include/${target}/${target}_features.h")
23+
set(export_file "include/${target}/${target}_export.h")
24+
set(template_export_file "include/${target}/${target}_api.h")
25+
set(export_macro "${target_id}_API")
2526

2627

2728
#
@@ -32,6 +33,8 @@ set(include_path "${CMAKE_CURRENT_SOURCE_DIR}/include/${target}")
3233
set(source_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
3334

3435
set(headers
36+
${include_path}/CTFibonacci.h
37+
${include_path}/CTFibonacci.inl
3538
${include_path}/Fibonacci.h
3639
)
3740

@@ -90,6 +93,10 @@ generate_export_header(${target}
9093
EXPORT_FILE_NAME ${export_file}
9194
EXPORT_MACRO_NAME ${export_macro}
9295
)
96+
generate_template_export_header(${target}
97+
${target_id}
98+
${template_export_file}
99+
)
93100

94101

95102
#
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
#pragma once
3+
4+
5+
#include <fiblib/fiblib_api.h>
6+
7+
8+
namespace fiblib
9+
{
10+
11+
12+
/**
13+
* @brief
14+
* Compile-time computation of fibonacci numbers
15+
*/
16+
template <unsigned long long i>
17+
class FIBLIB_TEMPLATE_API CTFibonacci
18+
{
19+
public:
20+
enum {
21+
value = CTFibonacci<i-2>::value + CTFibonacci<i-1>::value
22+
};
23+
};
24+
25+
26+
} // namespace fiblib
27+
28+
29+
#include <fiblib/CTFibonacci.inl>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
#pragma once
3+
4+
5+
namespace fiblib
6+
{
7+
8+
9+
template <>
10+
class FIBLIB_TEMPLATE_API CTFibonacci<0>
11+
{
12+
public:
13+
enum {
14+
value = 0
15+
};
16+
};
17+
18+
template <>
19+
class FIBLIB_TEMPLATE_API CTFibonacci<1>
20+
{
21+
public:
22+
enum {
23+
value = 1
24+
};
25+
};
26+
27+
28+
} // namespace fiblib

0 commit comments

Comments
 (0)