c – 嵌套的cmake库

我们有一个像这样组织的C/C++项目:

lib1/
    CMakeLists.txt
    sublib1/
        CMakeLists.txt
        foo.c
    log.c
    log.h

目录sublib1 /与其CMakeLists.txt文件中的add_library和target_link_libraries CMake命令链接.

更改:
我们无法从foo.c / some-func中调用log.c / some-func-2中的函数.链接器抱怨这些功能未定义.

解决方法

库和目标文件在链接命令中出现的顺序很重要,并且可能导致未定义符号的问题令人困惑.您没有提供太多信息,但请尝试告诉链接器将您的所有库视为一个组.如:

--start-group <all your libs> --end-group

从链接器文档:

Normally,an archive is searched only once in the order that it is specified on the command line. If a symbol in that archive is needed to resolve an undefined symbol referred to by an object in an archive that appears later on the command line,the linker would not be able to resolve that reference. By grouping the archives,they all be searched repeatedly until all possible references are resolved. Using this option has a significant performance cost. It is best to use it only when there are unavoidable circular references between two or more archives.

相关文章

一.C语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...