(平台:C ++ 14,Clang 3.8,Ubuntu 16.04,CMake 3.5.1)
我有两个静态库(PawLIB,CalikoCat)和一个依赖OtherLibrary的可执行文件。 CalkoCat中有一个单一类Dummy。 (我只是确保在开始对该库进行认真开发之前,所有内容都正确链接。)
Dummy类当前依赖于PawLIB的功能进行测试。calikocat-source/include/calikocat/dummy.hpp
#include "pawlib/iochannel.hpp"
class Dummy
{
public:
Dummy(){}
static void speak();
~Dummy(){}
};
calikocat-source/src/dummy.cpp
#include "otherlibrary/dummy.hpp"
void Dummy::speak()
{
pawlib::ioc << "Hello, world!" << pawlib::io_end;
}
没问题。它可以正确编译并与CMake链接。
注意:您可以放心地假设此问题中两个CMakeLists.txt文件中的所有变量都经过了仔细的测试和工作。我已经三遍了。
calikocat-source/CMakeLists.txt
(摘要)include_directories(include)
# Include headers of dependencies.
include_directories(${PAWLIB_DIR}/include)
add_library(${TARGET_NAME} STATIC
include/calikocat/dummy.hpp
src/dummy.cpp
)
# Link against dependencies.
target_link_libraries(${TARGET_NAME} ${PAWLIB_DIR}/lib/libpawlib.a)
现在,我喜欢在我的库中包含测试器应用程序,这也不例外。在回购中,我将库和测试程序代码分为
calikocat-source
和calikocat-tester
目录。我也将CMake用于测试仪...calikocat-tester/CMakeLists.txt
(摘要)include_directories(include)
# Include headers of dependencies.
include_directories(${PAWLIB_DIR}/include)
include_directories(../calikocat-source/include)
add_executable(calikocat-tester
main.cpp
)
# Link against dependencies.
target_link_libraries(${TARGET_NAME} ${CPGF_DIR}/lib/libcpgf.a)
target_link_libraries(${TARGET_NAME} ${PAWLIB_DIR}/lib/libpawlib.a)
target_link_libraries(${TARGET_NAME} ${CMAKE_HOME_DIRECTORY}/../calikocat-source/lib/$<CONFIG>/libcalikocat.a)
这是否正确构建完全取决于
main.cpp
上是否存在一行!请仔细阅读以下内容。calikocat-tester/main.cpp
,工作版本#include <calikocat/dummy.hpp>
#include <pawlib/iochannel.hpp>
int main()
{
pawlib::ioc << "Hello, world!" << pawlib::io_end;
Dummy::speak();
return 0;
}
编译并运行此版本可按预期工作,将
Hello, world!
打印两次到终端。calikocat-tester/main.cpp
,非工作版本#include <calikocat/dummy.hpp>
#include <pawlib/iochannel.hpp>
int main()
{
//pawlib::ioc << "Hello, world!" << pawlib::io_end;
Dummy::speak();
return 0;
}
我无法直接使用PawLIB注释掉该文件的此版本。这是错误...
[100%] Linking CXX executable ../../bin/Debug/calikocat-tester
../../../calikocat-source/lib/Debug/libcalikocat.a(dummy.cpp.o): In function `Dummy::speak()':
/home/jason/Code/Repositories/calikocat/calikocat-source/src/dummy.cpp:4: undefined reference to `pawlib::ioc'
/home/jason/Code/Repositories/calikocat/calikocat-source/src/dummy.cpp:5: undefined reference to `pawlib::iochannel::operator<<(pawlib::ioformat::IOControl const&)'
../../../calikocat-source/lib/Debug/libcalikocat.a(dummy.cpp.o): In function `pawlib::iochannel::operator<<(char const*)':
/home/jason/Code/Repositories/calikocat/calikocat-source/../../pawlib/pawlib/include/pawlib/iochannel.hpp:521: undefined reference to `pawlib::iochannel::resolve_pointer(char const*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/calikocat-tester.dir/build.make:99: recipe for target '../../bin/Debug/calikocat-tester' failed
我希望它可以编译,并且在运行时,只需将
Hello, world!
打印到终端一次即可。这是怎么回事,我想念什么?
最佳答案
您应该尝试反转这两行:
target_link_libraries(${TARGET_NAME} ${PAWLIB_DIR}/lib/libpawlib.a)
target_link_libraries(${TARGET_NAME} ${CMAKE_HOME_DIRECTORY}/../calikocat-source/lib/$<CONFIG>/libcalikocat.a)
因为libcalikocat.a使用的是libpawlib.a中的符号,所以它应该首先出现在命令行上。