CMake / CLion未定义对方法的引用

时间:2018-11-17 08:11:41

标签: c++ cmake clion

我具有以下文件结构:

enter image description here

我的CMakeList.txt包含:

cmake_minimum_required(VERSION 3.12)
project(Genetic)

set(CMAKE_CXX_STANDARD 17)
set(SOURCE_FILES "main.cpp"
    "genetic/DNA.cpp"
    "genetic/RandomGenerator.cpp")
#add_executable(Genetic genetic/main.cpp genetic/DNA.cpp genetic/DNA.h genetic/RandomGenerator.cpp genetic/RandomGenerator.h)
add_executable(Genetic ${SOURCE_FILES})

在main.cpp中,我尝试使用我的RandomGenerator:

#include <iostream>
#include "genetic/DNA.h"
#include "genetic/RandomGenerator.h"

int main() {
    auto randomGenerator = new RandomGenerator<float>(20, 0.1f, 0.9f);
    std::cout << randomGenerator->generate();
    delete randomGenerator;
    return 0;
}

编译时出现下一个错误:

"C:\Program Files\JetBrains\CLion 2018.2.4\bin\cmake\win\bin\cmake.exe" --build F:\CProjects\Genetic\cmake-build-debug --target Genetic -- -j 6
Scanning dependencies of target Genetic
[ 25%] Building CXX object CMakeFiles/Genetic.dir/main.cpp.obj
[ 50%] Linking CXX executable Genetic.exe
CMakeFiles\Genetic.dir/objects.a(main.cpp.obj): In function `main':
F:/CProjects/Genetic/main.cpp:11: undefined reference to `RandomGenerator<float>::RandomGenerator(int, float, float)'
F:/CProjects/Genetic/main.cpp:12: undefined reference to `RandomGenerator<float>::generate()'
F:/CProjects/Genetic/main.cpp:13: undefined reference to `RandomGenerator<float>::~RandomGenerator()'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\Genetic.dir\build.make:114: recipe for target 'Genetic.exe' failed
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/Genetic.dir/all' failed
mingw32-make.exe[3]: *** [Genetic.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Genetic.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Genetic.dir/rule] Error 2
CMakeFiles\Makefile2:83: recipe for target 'CMakeFiles/Genetic.dir/rule' failed
mingw32-make.exe: *** [Genetic] Error 2
Makefile:117: recipe for target 'Genetic' failed

为什么不起作用?我已经指定了cpp文件的路径。

已编辑#1

我添加了#include“ genetic / RandomGenerator.cpp”,它可以工作。但是CMake可以自己做...

#include <iostream>
#include "genetic/DNA.h"
#include "genetic/RandomGenerator.h"
#include "genetic/RandomGenerator.cpp"

int main() {
    auto randomGenerator = new RandomGenerator<float>(20, 0.1f, 0.9f);
    std::cout << randomGenerator->generate();
    delete randomGenerator;
    return 0;
}

0 个答案:

没有答案