我试图在CLION IDE中编译项目,但无法成功,但可以在Eclipse中编译完全相同的文件。
首先,这是错误:
CMakeFiles\Besart.dir/objects.a(library1.cpp.obj): In function `Init':
C:/Users/Besart/ClionProjects/Besart/Data Structures 1/HW 2/library1.cpp:17:
undefined reference to `DataStructure::DataStructure()'
现在,我已经包含了请求的文件,例如,在library1.cpp的源文件中
我有:
#include "library1.h"
#include "DataStructure.h"
void* Init() {
DataStructure* DS(nullptr);
try {
DS = new DataStructure();
} catch (std::bad_alloc &e) {
delete DS;
return nullptr;
}
return (void *)DS;
}
但是该文件在DataStructures.cpp中实现,并在DataStructure.h中声明
See image!!
最后是CMake:
cmake_minimum_required(VERSION 3.5)
project(Besart)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -std=c++11 )
set(SOURCE_FILES "Data Structures 1/HW 2/main.cpp"
"Data Structures 1/HW 2/Creature.cpp" "Data Structures 1/HW 2/Magi.cpp"
"Data Structures 1/HW 2/DataStructure.cpp/"
"Data Structures 1/HW 2/library1.cpp")
add_executable(Besart ${SOURCE_FILES} "Data Structures 1/HW 2/main.cpp"
"Data Structures 1/HW 2/Creature.cpp"
"Data Structures 1/HW 2/Magi.cpp"
"Data Structures 1/HW 2/DataStructure.cpp/"
"Data Structures 1/HW 2/library1.cpp")
CMAKE
有人可以看到问题并帮助我在CLion中编译该项目吗?
谢谢
最佳答案
您在DataStructure.cpp
之后有一个错误/:
set(SOURCE_FILES "Data Structures 1/HW 2/main.cpp"
"Data Structures 1/HW 2/Creature.cpp" "Data Structures 1/HW 2/Magi.cpp"
"Data Structures 1/HW 2/DataStructure.cpp/"
"Data Structures 1/HW 2/library1.cpp")
更改为:
set(SOURCE_FILES "Data Structures 1/HW 2/main.cpp"
"Data Structures 1/HW 2/Creature.cpp" "Data Structures 1/HW 2/Magi.cpp"
"Data Structures 1/HW 2/DataStructure.cpp"
"Data Structures 1/HW 2/library1.cpp")
然后通过以下方式修复您的add_executable():
add_executable(Besart ${SOURCE_FILES})
无需重复
${SOURCE_FILES}
的内容。关于c++ - undefined reference ``CLion,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40937914/