CMake链接不是子文件夹 [英] CMake link not subfolder

查看:148
本文介绍了CMake链接不是子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是cmake的新手.

I’m new to cmake.

我想创建代码来创建某些类(例如ClassA)的实例并将其收集在处理程序类中.为此,我创建了模板类Creator. 在每个类实现中,都使用Creator类创建该类的实例. (请参见ClassA.cpp第8行)

I want to create code to create instances of some classes (like ClassA) and collect them in a handler class. For this i have created a template class Creator. In each class implementation a instance of this class is created with Creator class. (see ClassA.cpp line 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

SubFolder/ClassA.h

#ifndef __CLASSA__
#define __CLASSA__

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

SubFolder/ClassA.cpp

SubFolder/ClassA.cpp

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

classA::classA()
{
}

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

SubFolder/Creator.h

SubFolder/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

SubFolder/CMakeLists.txt

add_library(SubFolder ClassA.cpp)

我编译该项目并运行它.所以我只得到输出"Hello,world!".
当我删除注释(main.cpp第5行)时,将使用ClassA的实例.所以我也得到了类Creator的输出. ClassA的代码已链接.
当我将类ClassA移到根目录时,它也起作用.

I compile this project and run it. So I get only the output "Hello, world!".
When I remove the comment (main.cpp line 5) a instance of ClassA is used. So I get also the output of class Creator. The code for ClassA is linked.
When I move the class ClassA to root directory it works also.

我还尝试过将PUBLIC_LINKdebuggeneral之类的参数用于target_link_libraries.但没有任何效果.

I have also tried to use parameters like PUBLIC_LINK, debug and general for target_link_libraries. But nothing works.

我打算在此main.cpp文件中使用一个Collection类,并从该集合中获取实例化的对象.在main.ccp文件中,我不想知道每个实例化的类,因为所有类ClassA ... ClassZ具有相同的接口(在此示例中未显示).

My intention use a Collection Class in this main.cpp file and get the instanced object from the 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).

如何强制链接未使用"的代码?

How can i force the link of "unused" code?

不知道是否有必要.我使用KDevelop4.

Do don't know if it's neccessary. I use KDevelop4.

推荐答案

请参见我已经使用GNU 4.8.1编译器测试了您的代码,在您的示例中,只需将target_link_libraries()行替换为:

I've tested your code with GNU 4.8.1 compilers and in your example just replace your target_link_libraries() line with:

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

来自target_link_libraries() 文档:

  • 链接标志:以-开头但不是-l或-framework的项目名称被视为链接器标志.请注意,出于传递依赖关系的目的,此类标志将与其他任何库链接项一样对待,因此通常可以安全地将它们指定为不会传播到依赖项的私有链接项.
  • A link flag: Item names starting with -, but not -l or -framework, are treated as linker flags. 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.

更多参考

相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆