运行cmake std教程步骤2示例时出错
我对cmake及其语法一无所知。但幸运的是,我能够按照以下链接中提到的介绍运行cmake教程步骤1: 但我在使用cmake运行Step2项目时完全被绊住了 我已经创建了step 2项目,并了解了链接库以进行数字平方根运算的语法,但我不了解如何运行该程序,因为我遇到以下错误:运行cmake std教程步骤2示例时出错,cmake,Cmake,我对cmake及其语法一无所知。但幸运的是,我能够按照以下链接中提到的介绍运行cmake教程步骤1: 但我在使用cmake运行Step2项目时完全被绊住了 我已经创建了step 2项目,并了解了链接库以进行数字平方根运算的语法,但我不了解如何运行该程序,因为我遇到以下错误: user@server:~/TER_CMAKE/Tutorial/step2_build$ cmake ../step2 CMake Error at CMakeLists.txt:19 (add_subdirectory
user@server:~/TER_CMAKE/Tutorial/step2_build$ cmake ../step2
CMake Error at CMakeLists.txt:19 (add_subdirectory):
The binary directory
/home/user/TER_CMAKE/Tutorial/step2/MathFunctions
is already used to build a source directory. It cannot be used to build
source directory
/home/user/TER_CMAKE/Tutorial/step2/MathFunctions
Specify a unique binary directory name.
-- Configuring incomplete, errors occurred!
在标题添加库(步骤2)下步骤2的以下位置提供了该示例
我的意图是以这种方式运行我的示例
step2_build$ cmake ../step2
step2_build$ cmake --build .
step2_build$ ./Tutorial 121
因为我不确定在这个平台上这样问是否好,但因为我没有任何其他的指导。我自己做这件事
注意:我不想使用任何工具来运行我的步骤2示例。我只想使用命令提示符和cmake命令来运行所有操作。在这里我可以理解cmake
编辑:
添加myCMakeLists.txt=
cmake_minimum_required(VERSION 3.5)
#set the project name
project(Tutorial VERSION 1.0)
#specify the c++ std
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
option(USE_MYMATH "Use tutorial provided math implementation" ON)
#Configure a header file to pass the version number to the source code
configure_file(TutorialConfig.h.in TutorialConfig.h)
#add the MathFunctions Library
add_subdirectory(MathFunctions)
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
endif()
#add the executable
add_executable(Tutorial tutorial.cpp)
target_link_libraries(Tutorial PUBLIC ${EXTRA_LIBS})
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
${EXTRA_LIBS}
)
My Sourcetutorial.cpp文件:
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#ifdef USE_MYMATH
#include "MathFunctions.h"
#endif
#include "TutorialConfig.h"
using namespace std;
int main(int argc, char* argv[])
{
if (argc < 2) {
cout << "Usage: " << argv[0] << " number" << endl;
return 1;
}
// convert input to double
const double inputValue = atof(argv[1]);
// calculate square root
#ifdef USE_MYMATH
const double outputValue = mysqrt(inputValue);
#else
const double outputValue = sqrt(inputValue);
#endif
cout << "The square root of " << inputValue << " is " << outputValue << endl;
return 0;
}
编辑:
Step2有一个文件夹MathFunctions,其中包含Cmake文件mysqrt.cpp文件
/TER\u CMAKE/Tutorial/step2/MathFunctions/CMakeLists.txt
add_library(MathFunctions mysqrt.cpp)
#include <iostream>
// a hack square root calculation using simple operations
double mysqrt(double x)
{
if (x <= 0) {
return 0;
}
double result = x;
// do ten iterations
for (int i = 0; i < 10; ++i) {
if (result <= 0) {
result = 0.1;
}
double delta = x - (result * result);
result = result + 0.5 * delta / result;
std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;
}
return result;
}
/teru-CMAKE/Tutorial/step2/MathFunctions/mysqrt.cpp
add_library(MathFunctions mysqrt.cpp)
#include <iostream>
// a hack square root calculation using simple operations
double mysqrt(double x)
{
if (x <= 0) {
return 0;
}
double result = x;
// do ten iterations
for (int i = 0; i < 10; ++i) {
if (result <= 0) {
result = 0.1;
}
double delta = x - (result * result);
result = result + 0.5 * delta / result;
std::cout << "Computing sqrt of " << x << " to be " << result << std::endl;
}
return result;
}
#包括
//使用简单操作的hack平方根计算
双mysqrt(双x)
{
如果设置了(xcaseUSE\u MYMATH
变量,则会调用两次add\u子目录(MathFunctions)
。您需要确定并删除CMakeLists.txt中第16行和第19行的其中一个事件。我可以看到两个问题:
当您使用-DUSE\u MYMATH=ON配置构建时,添加了两次子目录“MathFunctions”。这就是为什么您会得到“CMakeLists.txt处的CMake Error:19(添加子目录):”
修复,移除
#add the MathFunctions Library
add_subdirectory(MathFunctions)
依靠
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
endif()
在CMakeLists.txt文件中,您正在执行以下操作
而不是
${EXTRA_LIBS}
应该是
${EXTRA_INCLUDES}
我对这个cmake完全是新手。所以以这种方式提问。可能是我没有遵循适当的专业精神。但希望有人指导我运行这个step2示例。你提到的教程包含许多片段,其中一些片段无法一起使用。显示你的代码(CMakeLists.txt
)您正在尝试构建它。@Tsyvarev,我已经用我的cmake列表信息更新了我的帖子。我已经注释掉了第16行。但是如何运行我的代码。我已经在step2文件夹外创建了step2_build文件夹。并尝试使用以下方法运行代码:1)step2_build$cmake../step2(2)step2_build$cmake--build.(3)step2_build$。/Tutorial 121…但我无法获取./Tutorial可执行文件??我已使用MATHFUNCTION cmake文件和cpp文件更新了我的帖子实际上,如果您提到的所有步骤都成功完成,那么它必须在那里。尝试在您的构建目录中搜索该文件:step2_build$查找。-name Tutorial
,当我尝试在下面的命令下运行,我收到此错误。=>/TER\u CMAKE/Tutorial/step2\u build$CMAKE--build.make:**未指定目标,未找到makefile.Stop。仅此命令运行。/TER\u CMAKE/Tutorial/step2\u build$CMAKE../step2--配置完成--生成完成--生成文件已写入:/home/user/TER_CMAKE/Tutorial/step2我是否应该从其他文件夹位置运行它以生成./Tutorial可执行文件??