我正在测试CLion IDE,并且我试图编写一个最小的C ++程序。这是我的代码:
在main.cpp中:
#include "classings.h"
int main() {
classings s;
s.doSomething();
return 0;
}
在classings.h中:
class classings {
public:
void doSomething();
};
:
#include <string>
#include <iostream>
#include "classings.h"
void classings::doSomething() {
std::cout << "hei" << std::endl;
}
我不知道为什么这会给我这个错误:
Undefined symbols for architecture x86_64:
"classings::doSomething()", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我在OSX 10.10上。
答案 0 :(得分:0)
我认为您的源文件classings.h
和classings.cpp
未包含在CMakeLists.txt
中。
如果您打开CMakeLists.txt
,它应该如下所示:
不完整CMakeLists.txt
cmake_minimum_required(VERSION 2.8.4)
project(untitled)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp) # your other source files aren't listed
add_executable(my-program ${SOURCE_FILES})
您可以通过添加新的源文件
来解决问题 更正CMakeLists.txt
cmake_minimum_required(VERSION 2.8.4)
project(untitled)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp classings.hpp classings.cpp) # manually listing all sources
add_executable(my-program ${SOURCE_FILES})
仅供参考,每次使用file()或aux_source_directory()命令添加/删除新的源文件时,您都可以省去编辑CMakeLists.txt
的麻烦。这是一个例子:
set(SOURCE_DIRECTORY "src")
file(GLOB_RECURSE SOURCE_FILES "${SOURCE_DIRECTORY}/*.c" "${SOURCE_DIRECTORY}/*.h"
"${SOURCE_DIRECTORY}/*.cc" "${SOURCE_DIRECTORY}/*.hh"
"${SOURCE_DIRECTORY}/*.cpp" "${SOURCE_DIRECTORY}/*.hpp"
"${SOURCE_DIRECTORY}/*.cxx" "${SOURCE_DIRECTORY}/*.hxx")
add_executable(my-program ${SOURCE_FILES})
或
aux_source_directory("src" SOURCE_FILES)
add_executable(my-program ${SOURCE_FILES})