Clion对函数的未定义引用

Tom*_*k44 5 c++ reference undefined clion

我的main.cpp,头文件func.h和另一个源文件func.cpp有一个简单的程序。我使用CLion 2016.3。我的编译器是gcc。

他们看起来像这样:

Main.cpp

#include <iostream>
#include <stdlib.h>
#include <cstdio>
#include "func.h"

int main() {

int c;
c = number(2);
printf("%i", c);

}
Run Code Online (Sandbox Code Playgroud)

func.cpp

int number(int a){

return a;

}
Run Code Online (Sandbox Code Playgroud)

func.h

#ifndef TEST2_FUNC_H
#define TEST2_FUNC_H

int number(int a);

#endif //TEST2_FUNC_H
Run Code Online (Sandbox Code Playgroud)

我的cmakelists.txt

cmake_minimum_required(VERSION 3.6)
project(test2)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(test2 ${SOURCE_FILES})
Run Code Online (Sandbox Code Playgroud)

如果我运行构建,则会出现以下错误:

CMakeFiles\test2.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/name/ClionProjects/test2/main.cpp:8: undefined reference to `number(int)'
....
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?我已经搜索了其他类似的问题并找到了一些解决方案,但是它们对我不起作用或者我不知道该怎么办。实际上,我在C-Project中遇到这个问题,但是问题是相同的,我认为解决方案将是相同的。
你能帮我么?
非常感谢你。

See*_*ddo 4

Make sure you include func.h in your main

#include "func.h"
Run Code Online (Sandbox Code Playgroud)

and put 'func.cpp' in CMakeList.txt set source

List the sources explicitly here. That is all (.cpp, .c ,.cc) to compile together. If the sources files are not in the current directory, then you have to specify the full path to the source files

set(SOURCE_FILES main.cpp func.cpp)
Run Code Online (Sandbox Code Playgroud)

file(GLOB SOURCE_FILES *.cpp)如果您想在编译中自动添加文件,则可以使用。但请记住,强烈不鼓励这样做。