我目前在和柯南在一起。因此,我创建了两个小项目:
第一个项目是一个小的库“fcdk”:https://github.com/lmarzull/fcdk/tree/devel
第二个是一个包含柯南和circle-ci内容的项目(尚不知道我是否应该将库/柯南/ circle-ci归为一个项目,但这还不是重点)。
第二个项目称为“fcdk-conan”
我决定将库的单元测试放入“test_package”目录下的fcdk-conan项目中。我这样做是为了避免在我的“fcdk”库中依赖于Google测试,而是在fcdk-conan项目中具有这种依赖关系(不知道这是一个好主意)
我创建了一个非常小的测试程序:
#include <iostream>
int
main()
{
std::cout << "Hello, world!" << std::endl;
}
一切都很好。
但是现在,我想为我的库添加一些单元测试。因此,我需要查找/编译/链接“fcdk”库。因此,我将main.cc文件更改为以下文件:
#include <iostream>
#include <fcdk/CommandLineOptionFlag.h>
int
main()
{
FCDK::CommandLineOptionFlag show_help('h', "help", "show this help message");
std::cout << "Hello, world!" << std::endl;
}
这是test_package目录的CMakeLists.txt:
cmake_minimum_required(VERSION 3.2)
project(FcdkTest CXX)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
add_executable(
test-fcdk
main.cc
)
target_include_directories(test-fcdk PUBLIC ${CONAN_INCLUDE_DIRS_FCDK})
target_link_libraries(test-fcdk PUBLIC ${CONAN_LIBS_FCDK})
target_link_libraries(test-fcdk PUBLIC CONAN_PKG::fcdk)
enable_testing()
add_test(NAME test-fcdk
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin
COMMAND test-fcdk)
和柯南的食谱:
import os
from conans import ConanFile, CMake, tools
class FcdkTestConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
generators = "cmake"
requires = "fcdk/1.0.0"
def build(self):
cmake = CMake(self)
cmake.configure()
cmake.build()
def test(self):
if not tools.cross_building(self.settings):
os.chdir("bin")
self.run(".%stest-fcdk" % os.sep)
我无法实现test_package以正确链接。
CMakeFiles/test-fcdk.dir/main.cc.o: In function `FCDK::CommandLineOptionFlag::accept(FCDK::VisitorBase&)':
main.cc:(.text._ZN4FCDK21CommandLineOptionFlag6acceptERNS_11VisitorBaseE[_ZN4FCDK21CommandLineOptionFlag6acceptERNS_11VisitorBaseE]+0xa1): undefined reference to `FCDK::demangleTypename(char const*)'
main.cc:(.text._ZN4FCDK21CommandLineOptionFlag6acceptERNS_11VisitorBaseE[_ZN4FCDK21CommandLineOptionFlag6acceptERNS_11VisitorBaseE]+0xdd): undefined reference to `FCDK::demangleTypename(char const*)'
CMakeFiles/test-fcdk.dir/main.cc.o: In function `main':
main.cc:(.text.startup+0x5e): undefined reference to `FCDK::CommandLineOptionFlag::CommandLineOptionFlag(char, std::string, std::string)'
先前所有步骤:
conan source
conan install
conan build
conan package
conan export-pkg
很好,对我来说似乎正确。我在这里放置了柯南包命令的内容:
package/
package/conaninfo.txt
package/include
package/include/fcdk
package/include/fcdk/Exception.h
package/include/fcdk/CommandLineOption.h
package/include/fcdk/CommandLineOptionWithValue.h
package/include/fcdk/Visitor.h
package/include/fcdk/ABI.h
package/include/fcdk/CommandLineParser.h
package/include/fcdk/CommandLineOptionFlag.h
package/conanmanifest.txt
package/lib
package/lib/libfcdk.a
package/share
package/share/cmake
package/share/cmake/fcdk
package/share/cmake/fcdk/fcdkTargets.cmake
package/share/cmake/fcdk/fcdkTargets-release.cmake
我还查看了libfcdk.a中缺少的符号
例如:
U FCDK::demangleTypename[abi:cxx11](char const*)
U FCDK::demangleTypename[abi:cxx11](char const*)
0000000000000000 t _GLOBAL__sub_I__ZN4FCDK16demangleTypenameB5cxx11EPKc
0000000000000000 T FCDK::demangleTypename[abi:cxx11](char const*)
当我使用VERBOSE = 1运行make时,在链接命令上看不到fcdk库信息
/usr/bin/cmake -E cmake_link_script CMakeFiles/test-fcdk.dir/link.txt --verbose=1
/usr/bin/c++ -m64 -O3 -DNDEBUG -rdynamic CMakeFiles/test-fcdk.dir/main.cc.o -o bin/test-fcdk
CMakeFiles/test-fcdk.dir/main.cc.o: In function `FCDK::CommandLineOptionFlag::accept(FCDK::VisitorBase&)':
有人可以帮我弄清楚为什么test_pacakge食谱无法再次链接我的fcdk / 1.0.0软件包吗?
非常感谢你
编辑:更新了test_pacakge的柯南存储库
https://github.com/lmarzull/fcdk-conan/tree/devel
最佳答案
首先,非常感谢@ymochurad
他指出我要用给定库的名称填充self.cpp_info.libs。
但是这样做之一,故事继续。我需要修改
test_package / CMakeLists.txt
test_package / conanfile.py
# conanfile.py
def package_info(self):
self.cpp_info.libs = [ "fcdk" ]
和
# test_package/CMakeLists.txt
FIND_PACKAGE(fcdk REQUIRED)
# remove conan_basic_setup(TARGETS)
# and replace with:
conan_basic_setup()
# Add ${CONAN_INCLUDE_DIRS}
target_include_directories(test-fcdk
PRIVATE ${CONAN_INCLUDE_DIRS})
# and also fcdk_LIBRARIES in link directive
target_link_libraries(test-fcdk ${fcdk_LIBRARIES})
和
# test_package/conanfile.py
def config_options(self):
self.settings.compiler.libcxx = "libstdc++11"
所有这些修改解决了最初的问题。
关于c++ - 创建柯南test_package配方,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58881833/