- #include
-
- int Callback_1() // Callback Function 1
- {
- printf("Hello, this is Callback_1 \n");
- return 0;
- }
-
- int Callback_2() // Callback Function 2
- {
- printf("Hello, this is Callback_2 \n");
- return 0;
- }
-
- int Callback_3() // Callback Function 3
- {
- printf("Hello, this is Callback_3 \n");
- return 0;
- }
-
- int Handle(int (*Callback)()) //函数指针
- {
- printf("Entering Handle Function. \n");
- Callback();
- printf("Leaving Handle Function. \n");
- }
-
- int main()
- {
- printf("Entering Main Function. \n");
- Handle(Callback_1);
- Handle(Callback_2);
- Handle(Callback_3);
- printf("Leaving Main Function. \n");
- return 0;
- }
- /tmp/tmp.BI8jCkdUAx/cmake-build-debug/openssl-demo
- Entering Main Function.
- Entering Handle Function.
- Hello, this is Callback_1
- Leaving Handle Function.
- Entering Handle Function.
- Hello, this is Callback_2
- Leaving Handle Function.
- Entering Handle Function.
- Hello, this is Callback_3
- Leaving Handle Function.
- Leaving Main Function.
-
- 进程已结束,退出代码0
- #ifndef OPENSSL_DEMO_OPENSSL_TEST_H
- #define OPENSSL_DEMO_OPENSSL_TEST_H
- typedef int *callback_random(char* random,int len);
- void set_callback(callback_random *cb);
- int generate_random(char* random,int len);
- #endif //OPENSSL_DEMO_OPENSSL_TEST_H
- #include "openssl-test.h"
- #include
- #include
-
- callback_random *cb_rand = nullptr;
-
- static int default_random(char* random,int len){
- memset(random,0x01,len);
- }
-
- void set_callback(callback_random *cb){
- cb_rand = cb;
- }
-
- int generate_random(char* random,int len){
- if (cb_rand == nullptr){
- return default_random(random,len);
- } else{
- return *cb_rand(random,len);
- }
- }
- #include
- #include
- #include "openssl-test.h"
-
- static int * my_rand(char* rand, int len){
- memset(rand,0x02,len);
- }
- int main()
- {
- char random[10];
- int ret = 0;
- /*
- * 如果注释掉 set_callback ,打印输出的数据均为 1,即没有使用自定义的函数进行回调
- * 如果用户自定义随机数函数,就会使用用户自定义的函数,而不是默认的函数
- */
- // set_callback(my_rand);
- ret = generate_random(random,10);
- for (int i = 0; i < 10; ++i) {
- printf("%d ",random[i]);
- }
- return 0;
- }
- cmake_minimum_required(VERSION 3.15)
-
- project(openssl-demo)
- set(CMAKE_CXX_STANDARD 11)
-
- # 忽略警告
- set(CMAKE_CXX_FLAGS "-Wno-error=deprecated-declarations -Wno-deprecated-declarations ")
-
- # 指定lib目录
- link_directories(/usr/local/gmssl/lib)
-
- # 指定头文件搜索策略
- include_directories(/usr/local/gmssl/include)
-
- # 使用指定的源文件来生成目标可执行文件
- add_executable(${PROJECT_NAME} main.cpp openssl-test.h openssl-test.cpp)
-
- # 将库链接到项目中
- target_link_libraries(${PROJECT_NAME} ssl crypto pthread dl)