如何解决未定义对`func'的引用多文件编译
我对使用c ++进行多文件编译非常陌生,我正在尝试通过测试来理解它。
但是在下面的示例中,我收到错误undefined reference to `func()'
,但我不明白为什么。
预先感谢!
Main.cpp
#include <iostream>
#include "file1.h"
int main() {
func();
return 0;
}
File1.h
#ifndef UNTITLED1_FILE1_H
#define UNTITLED1_FILE1_H
#include <iostream>
void func();
#endif //UNTITLED1_FILE1_H
File1.cpp
#include "file1.h"
void func() {
std::cout << "Test" << std::endl;
}
CMAKELIST
cmake_minimum_required(VERSION 3.17)
project(untitled1)
set(CMAKE_CXX_STANDARD 14)
add_executable(untitled1 main.cpp file1.h)
解决方法
基本上,您还没有包含所有源文件。另外,您还没有指定包含目录。
cmake_minimum_required(VERSION 3.17)
# Never name your project the same thing as any of your targets
project(COOL_PROJECT)
set(CMAKE_CXX_STANDARD 14)
add_executable(untitled1)
# You must include all source files,header files are optional.
# However adding header files explicitly will result in better IDE support
#
# Prefer to add source files using target_sources instead
target_sources(untitled1 PRIVATE
main.cpp
file1.cpp
file1.h
)
# Add your current directory as an include directory.
target_include_directories(untitiled PRIVATE ${CMAKE_CURRENT_LIST_DIR})
顺便说一句,套管很重要。因此,请确保文件名大小写匹配。
File1.cpp!= file1.cpp
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。