我是创建 CMake 文件的新手 我已经搜索了很多,但我仍然不明白为什么我不能使用外部库中的任何函数,但可以使用头文件中的#define变量
我正在尝试添加此库:https://github.com/pq-crystals/dilithium
我的CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(react-native-dilithium VERSION 1.0.0)
include(FetchContent)
FetchContent_Declare(dilithium
GIT_REPOSITORY https://github.com/pq-crystals/dilithium.git
GIT_TAG master
)
FetchContent_MakeAvailable(dilithium)
add_library(dilithium SHARED)
target_sources(dilithium PUBLIC ${dilithium_SOURCE_DIR})
set_target_properties(dilithium PROPERTIES LINKER_LANGUAGE CXX PROPERTIES DILITHIUM_MODE 5 PROPERTIES DILITHIUM_USE_AES TRUE)
add_executable(cpp_test main.cpp)
target_include_directories(cpp_test PUBLIC
${dilithium_BINARY_DIR}
${dilithium_SOURCE_DIR}/ref
)
target_link_libraries(cpp_test PUBLIC dilithium)
我希望这能起作用:
#include <iostream>
#include <api.h>
using namespace std;
int main(int argc, char* argv[]) {
// Message to sign
const char* message = "This is a message to be signed.";
size_t message_len = strlen(message);
// Private key (replace with your actual key)
unsigned char private_key[pqcrystals_dilithium5aes_ref_SECRETKEYBYTES];
// Signature buffer (allocate enough space based on parameter set)
unsigned char signature[pqcrystals_dilithium5aes_ref_BYTES];
size_t signature_len = sizeof(signature);
// Call the signing function
int result = pqcrystals_dilithium5aes_ref_signature(
signature, &signature_len,
(const unsigned char*)message, message_len,
private_key
);
return 0;
}
但是当我尝试使用 pqcrystals_dilithium5aes_ref_signature 时发生错误
错误:
ld: Undefined symbols:
pqcrystals_dilithium5aes_ref_signature(unsigned char*, unsigned long*, unsigned char const*, unsigned long, unsigned char const*), referenced from:
_main in main.cpp.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [cpp_test] Error 1
make[1]: *** [CMakeFiles/cpp_test.dir/all] Error 2
make: *** [all] Error 2
既然库作者不关心 C++,你就必须关心他们。改变
#include <api.h>
到
extern "C" {
#include <api.h>
}