add_library没有使用C源代码创建.lib

时间:2019-02-22 11:10:31

标签: c++ c cmake bcrypt

我正在尝试使用库https://github.com/trusch/libbcrypt中的crypt blowfish项目周围的c包装器,以及两个文件bcrypt.c和bcrypt.h的cpp包装器。我从源代码创建了一个bcryptcpp.lib,但是当将它们与可执行文件一起使用时,会出现未定义的符号错误。 这是我的cmakelists.txt-

# BCrypt CPP
enable_language(ASM)

set(CMAKE_ASM_FLAGS "${CXXFLAGS} -x assembler-with-cpp")

include_directories( ${CMAKE_CURRENT_SOURCE_DIR})
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/crypt_blowfish-1.3)

set(BCRYPT_SOURCES
    ${CMAKE_CURRENT_SOURCE_DIR}/BCrypt.hpp
    ${CMAKE_CURRENT_SOURCE_DIR}/BCrypt.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/WinBCrypt.hpp
    ${CMAKE_CURRENT_SOURCE_DIR}/bcrypt.c
    ${CMAKE_CURRENT_SOURCE_DIR}/bcrypt.h
    ${CMAKE_CURRENT_SOURCE_DIR}/crypt_blowfish-1.3/crypt_blowfish.c
    ${CMAKE_CURRENT_SOURCE_DIR}/crypt_blowfish-1.3/crypt_blowfish.h
    ${CMAKE_CURRENT_SOURCE_DIR}/crypt_blowfish-1.3/crypt_gensalt.c
    ${CMAKE_CURRENT_SOURCE_DIR}/crypt_blowfish-1.3/crypt_gensalt.h
    ${CMAKE_CURRENT_SOURCE_DIR}/crypt_blowfish-1.3/wrapper.c
    ${CMAKE_CURRENT_SOURCE_DIR}/crypt_blowfish-1.3/x86.S
)

add_library(bcryptcpp
    STATIC
        ${BCRYPT_SOURCES})

set_target_properties(bcryptcpp
    PROPERTIES
        VERSION "${PROJECT_VERSION}"
        PUBLIC_HEADER BCrypt.hpp
        LINKER_LANGUAGE CXX
        FOLDER "BCrypt"
)

Visual Studio生成的bcryptcpp.lib仅2kb。不会编译c文件吗?这里可能是什么问题?

以下是错误,这些错误是在编译仅包含上述库链接(BCrypt.hpp,BCrypt.cpp,winbcrypt.h)中的3个标头的测试可执行文件时生成的-

Severity    Code    Description Project File    Line    Suppression State
Error   LNK1120 3 unresolved externals  BCryptTest  C:\Program Files (x86)\Horizon\test\Debug\BCryptTest.exe    1   
Error   LNK2019 unresolved external symbol _bcrypt_gensalt referenced in function "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl BCrypt::generateHash(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (?generateHash@BCrypt@@SA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@H@Z)    BCryptTest  C:\Users\sagun\horizon-cpp\build\src\Tests\BCryptTest.obj   1   
Error   LNK2019 unresolved external symbol _bcrypt_hashpw referenced in function "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl BCrypt::generateHash(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (?generateHash@BCrypt@@SA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV23@H@Z) BCryptTest  C:\Users\sagun\horizon-cpp\build\src\Tests\BCryptTest.obj   1   
Error   LNK2019 unresolved external symbol _bcrypt_checkpw referenced in function "public: static bool __cdecl BCrypt::validate_password(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?validate_password@BCrypt@@SA_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) BCryptTest  C:\Users\sagun\horizon-cpp\build\src\Tests\BCryptTest.obj   1   

dumpbin /EXPORTS bcryptcpp.lib的输出未显示任何导出,因此我猜测其编译错误-

Dump of file bcryptcpp.lib

File Type: LIBRARY

  Summary

          20 .chks64
         43C .debug$S
          74 .debug$T
          30 .drectve

1 个答案:

答案 0 :(得分:0)

为了解问题原因( CMakeLists.txt 或另一个项目中的错误链接),您需要检查您的 .lib 文件。 尝试通过dumpbin bcryptcpp.lib 中找到导出符号。 打开可视命令控制台并运行下一个命令:

46.181.252.151  - - [22/Feb/2019:19:04:19 +0000] "GET /task/768476366 HTTP/1.1" 200 6765
36.231.298.259  - - [22/Feb/2019:19:04:20 +0000] "GET /doc/wallet/9855563 HTTP/1.1" 200 45564

如果在 exports 标头下看到函数,则表明您的库已正确编译,您需要检查库的体系结构( x86 x64 < / strong>)-也许您将错误的库链接到另一个项目。

在另一种情况下, CMakeLists.txt 中的问题。