__attribute__((visibility(“default“)))

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 = %d\n", a, b, c);

#ifdef FBC_EXPORT
	fprintf(stdout, "dynamic library: value: %d\n", 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 = %d\n", 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: %s\n", str3.c_str());

	return 0;
}

int test_library_3()
{
#ifdef FBC_EXPORT
	fprintf(stdout, "dynamic library cann't run print_log function\n");
#else
	print_log();
#endif

	return 0;
}

} // namespace test_library_

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值