朋友们,我有一个非常非常简单的 CMAKE 项目,其结构如下:
In the root directory:
CMakeLists.txt
include_ (directory)
Hello.h
src (directory)
CMakeLists.txt
Hello.cpp
CmakeTestProject.cpp
这是根目录下的CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(CMakeTestProject VERSION 1.0)
set(CMAKE_CXX_STANDARD 17)
set(MY_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include_)
add_subdirectory(src)
这是 src 目录中的 CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
add_executable (CMakeTestProject CMakeTestProject.cpp ${MY_INCLUDE_DIR}/Hello.h Hello.cpp)
target_include_directories(CMakeTestProject PUBLIC ${MY_INCLUDE_DIR})
CMakeTestProject.cpp:
#include <iostream>
#include "Hello.h"
using namespace std;
int main()
{
Hello h;
h.show();
return 0;
}
你好.h:
#pragma once
#include <iostream>
class Hello
{
public:
Hello() {}
void show();
};
你好.cpp:
#include "Hello.h"
void Hello::show()
{
std::cout << "Hello world!";
}
错误:
*1. CMake Error at CMakeTestProject/src/CMakeLists.txt:4 (add_executable):
No SOURCES given to target: CMakeTestProject E:\Project\CMakeTestProject\CMakeTestProject/src/CMakeLists.txt 4
2. CMake Error at CMakeTestProject/src/CMakeLists.txt:4 (add_executable):
Cannot find source file:
E:/Project/CMakeTestProject/include_/Hello.h
Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm .h
.hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90 .f95 .f03 .hip .ispc E:\Project\CMakeTestProject\CMakeTestProject/src/CMakeLists.txt 4
*
我尝试处理 CMAKE,但在非常简单的情况下被阻止。顺便说一句,我看到的所有类似的例子都效果很好,但不是我的)。据我了解,我的代码中有一个非常愚蠢的错误,但不知道在哪里。请。帮助)
复制粘贴源代码后,项目在我的 Ubuntu 计算机上正确构建并执行,您确定在目录/文件名中没有犯任何错误吗?您能否发送 ls 或 tree 命令的输出来确保情况确实如此。
$ cmake --version
cmake version 3.16.3