简体   繁体   English

CMake链接不是子文件夹

[英]CMake link not subfolder

I'm new to cmake. 我是cmake的新手。

I want to create code to create instances of some classes (like ClassA ) and collect them in a handler class. 我想创建代码来创建某些类(例如ClassA )的实例并将其收集在处理程序类中。 For this i have created a template class Creator . 为此,我创建了一个模板类Creator In each class implementation a instance of this class is created with Creator class. 在每个类实现中,都使用Creator类创建该类的实例。 (see ClassA.cpp line 8) (请参阅ClassA.cpp第8行)

I have following folder structure 我有以下文件夹结构

├── CMakeLists.txt
├── main.cpp
└── SubFolder
    ├── ClassA.cpp
    ├── ClassA.h
    ├── CMakeLists.txt
    └── Creator.h

./main.cpp ./main.cpp

#include <iostream>
#include "SubFolder/ClassA.h"

int main(int argc, char **argv) {
   //classA a;

    std::cout << std::endl << "Hello, world!" << std::endl;
    return 0;
}

./CMakeLists.txt ./CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(teststaticcmake)

add_executable(teststaticcmake main.cpp)
add_subdirectory(SubFolder)
target_link_libraries(teststaticcmake SubFolder)

install(TARGETS teststaticcmake RUNTIME DESTINATION bin)

SubFolder/ClassA.h 子文件夹/ ClassA.h

#ifndef __CLASSA__
#define __CLASSA__

class classA
{
    public:
        classA();
};
#endif //__CLASSA__

SubFolder/ClassA.cpp 子文件夹/ ClassA.cpp

#include "ClassA.h"
#include "Creator.h"

classA::classA()
{
}

classA* pClassA = Creator<classA>::create();

SubFolder/Creator.h 子文件夹/ Creator.h

#ifndef __CREATOR__
#define __CREATOR__

#include <iostream>

template<typename T>
class Creator
{
    public:
        static T* create()
        {
            T* p = new T();

            // Do Something here
            // ... like output
            std::cout << std::endl << "created: " << p;

            return p;
        }
};
#endif //__CREATOR__

SubFolder/CMakeLists.txt 子文件夹/的CMakeLists.txt

add_library(SubFolder ClassA.cpp)

I compile this project and run it. 我编译该项目并运行它。 So I get only the output "Hello, world!". 因此,我仅获得输出“ Hello,world!”。
When I remove the comment ( main.cpp line 5) a instance of ClassA is used. 当我删除注释( main.cpp第5行)时,将使用ClassA的实例。 So I get also the output of class Creator . 所以我也得到了Creator类的输出。 The code for ClassA is linked. ClassA的代码已链接。
When I move the class ClassA to root directory it works also. 当我将ClassA类移到根目录时,它也起作用。

I have also tried to use parameters like PUBLIC_LINK , debug and general for target_link_libraries. 我也曾尝试对PUBLIC_LINK使用PUBLIC_LINKdebuggeneral等参数。 But nothing works. 但是什么都行不通。

My intention use a Collection Class in this main.cpp file and get the instanced object from the collection. 我的意图是在此main.cpp文件中使用Collection类,并从集合中获取实例对象。 In the main.ccp file i don't want to know each instanced class because all class ClassA ... ClassZ have the same interface (not shown in this example). 在main.ccp文件中,我不想知道每个实例化的类,因为所有类ClassA ... ClassZ具有相同的接口(在此示例中未显示)。

How can i force the link of "unused" code? 如何强制链接“未使用”的代码?

Edit: Do don't know if it's neccessary. 编辑:不知道这是否必要。 I use KDevelop4. 我使用KDevelop4。

See How to force gcc to link an unused static library 请参阅如何强制gcc链接未使用的静态库

I've tested your code with GNU 4.8.1 compilers and in your example just replace your target_link_libraries() line with: 我已经使用GNU 4.8.1编译器测试了您的代码,在您的示例中,只需将target_link_libraries()行替换为:

target_link_libraries(
    teststaticcmake 
    PRIVATE
        "-Wl,--whole-archive" 
        SubFolder 
        "-Wl,--no-whole-archive"
)

From target_link_libraries() documentation : target_link_libraries() 文档中

  • A link flag : Item names starting with -, but not -l or -framework, are treated as linker flags. 链接标志 :以-开头但不是-l或-framework的项目名称被视为链接器标志。 Note that such flags will be treated like any other library link item for purposes of transitive dependencies, so they are generally safe to specify only as private link items that will not propagate to dependents. 请注意,出于传递依赖关系的目的,将此类标志与任何其他库链接项一样对待,因此通常可以安全地将它们指定为不会传播给依赖项的私有链接项。

More References 更多参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM