概述
attribute((visibility(“default”)))//hidden决定变成动态库后,是否可被访问执行。
#ifndef FBC_LIBRARY_LIBRARY_HPP_
#define FBC_LIBRARY_LIBRARY_HPP_
// reference: https://gcc.gnu.org/wiki/Visibility
// https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/CppRuntimeEnv/Articles/SymbolVisibility.html
//#define FBC_EXPORT
#ifdef __GNUC__ >= 4 // it means the compiler is GCC version 4.0 or later
#ifdef FBC_EXPORT
#warning "===== dynamic library ====="
#define FBC_API_PUBLIC __attribute__((visibility ("default")))
#define FBC_API_LOCAL __attribute__((visibility("hidden")))
#else
#warning "===== static library ====="
#define FBC_API_PUBLIC
#define FBC_API_LOCAL
#endif
#else
#error "##### requires gcc version >= 4.0 #####"
#endif
#ifdef __cplusplus
extern "C" {
#endif
FBC_API_PUBLIC int library_add(int a, int b);
FBC_API_LOCAL void print_log();
#ifdef FBC_EXPORT
FBC_API_PUBLIC int value;
#endif
#ifdef __cplusplus
}
#endif
template<typename T>
class FBC_API_PUBLIC Simple {
public:
Simple() = default;
void Init(T a, T b);
T Add() const;
private:
T a, b;
};
#endif // FBC_LIBRARY_LIBRARY_HPP_
#include "test_library.hpp"
#include <iostream>
#include <string>
#include <library.hpp>
namespace test_library_ {
int test_library_1()
{
int a{ 4 }, b{ 5 }, c{ 0 };
c = library_add(a, b);
fprintf(stdout, "%d + %d = %dn", a, b, c);
#ifdef FBC_EXPORT
fprintf(stdout, "dynamic library: value: %dn", value);
#endif
return 0;
}
int test_library_2()
{
Simple<int> simple1;
int a{ 4 }, b{ 5 }, c{ 0 };
simple1.Init(a, b);
c = simple1.Add();
fprintf(stdout, "%d + %d = %dn", a, b, c);
Simple<std::string> simple2;
std::string str1{ "csdn blog: " }, str2{ "http://blog.csdn.net/fengbingchun" }, str3;
simple2.Init(str1, str2);
str3 = simple2.Add();
fprintf(stdout, "contents: %sn", str3.c_str());
return 0;
}
int test_library_3()
{
#ifdef FBC_EXPORT
fprintf(stdout, "dynamic library cann't run print_log functionn");
#else
print_log();
#endif
return 0;
}
} // namespace test_library_
最后
以上就是震动汉堡为你收集整理的__attribute__((visibility(“default“)))的全部内容,希望文章能够帮你解决__attribute__((visibility(“default“)))所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复