微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

简单的 CMAKE 教程有问题

如何解决简单的 CMAKE 教程有问题

我只是按照 youtube 上的简单 CMake 教程进行操作 https://www.youtube.com/watch?v=V1YP7eJHDJE

正如你在 YouTube 上看到的, 文件夹目录是

CMAKE-GOOD
-build
-CMakeLists.txt
-main.cpp

这是 CMakeLists.txt

cmake_minimum_required(VERSION 3.14)

project(MyProject VERSION 1.0.0)

add_executable(cmake-good main.cpp)

这是 main.cpp

#include <iostream>
int main()
{
    std::cout << "hello,world! \n";
}

这是我编写的唯一源代码。 从这部分开始,这将涵盖我遇到的错误

edit) 我也在构建目录中做了 cmake .. 结果如下图

(base) bmssa@bmssa:~/Desktop/CMake_tutorial/CMAKE-GOOD/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/bmssa/Desktop/CMake_tutorial/CMAKE-GOOD/build

当我在 cmake --build . 中输入 ~Desktop/CMake_tutorial_CMAKE_GOOD/build 时,它显示一个错误

(base) bmssa@bmssa:~/Desktop/CMake_tutorial/CMAKE-GOOD/build$ cmake --build .
[ 50%] Linking CXX executable cmake-good
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
CMakeFiles/cmake-good.dir/build.make:96: recipe for target 'cmake-good' Failed
make[2]: *** [cmake-good] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/cmake-good.dir/all' Failed
make[1]: *** [CMakeFiles/cmake-good.dir/all] Error 2
Makefile:90: recipe for target 'all' Failed
make: *** [all] Error 2

我听说 cmake --build 。类似于 make, 所以我尝试了 make 并且它显示了这样的错误

(base) bmssa@bmssa:~/Desktop/CMake_tutorial/CMAKE-GOOD/build$ make
[ 50%] Linking CXX executable cmake-good
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
CMakeFiles/cmake-good.dir/build.make:96: recipe for target 'cmake-good' Failed
make[2]: *** [cmake-good] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/cmake-good.dir/all' Failed
make[1]: *** [CMakeFiles/cmake-good.dir/all] Error 2
Makefile:90: recipe for target 'all' Failed
make: *** [all] Error 2

当我输入 cmake --version 时,

cmake version 3.20.0

CMake suite maintained and supported by Kitware (kitware.com/cmake).

你能帮我解决这个问题吗? 我什么也做不了,因为我什至无法在非常简单的教程中解决这个简单的错误。 请帮助我 谢谢

ps。 因为我什至不知道 CMake 中的错误是什么,你能告诉我我应该在 google 中输入什么来解决这个问题吗?

目标食谱? 目标“全部”的配方失败?

我不知道我应该在 google 中输入什么....

edit2) 我也做了 cmake -S . -B build/foobarcmake --build build/foobar 这也显示了类似的错误

(base) bmssa@bmssa:~/Desktop/CMake_tutorial/CMAKE-GOOD$ cmake -S . -B build/foobar
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/bmssa/Desktop/CMake_tutorial/CMAKE-GOOD/build/foobar
(base) bmssa@bmssa:~/Desktop/CMake_tutorial/CMAKE-GOOD$ cmake --build build/foobar
[ 50%] Building CXX object CMakeFiles/cmake-good.dir/main.cpp.o
[100%] Linking CXX executable cmake-good
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
CMakeFiles/cmake-good.dir/build.make:96: recipe for target 'cmake-good' Failed
make[2]: *** [cmake-good] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/cmake-good.dir/all' Failed
make[1]: *** [CMakeFiles/cmake-good.dir/all] Error 2
Makefile:90: recipe for target 'all' Failed
make: *** [all] Error 2

解决方法

这是我对您的问题的猜测。 CMake 无法找到工作工具链(编译器、链接器等)

尝试通过指定环境变量 CC 和 CXX 来帮助它。

export CC = /foobar/...../gcc
export CXX = /foobar/...../g++

这里是 CMake 查找编译器的方法(根据 CMake 开发人员的说法),供将来参考

  1. CMAKE_TOOLCHAIN_FILE(如果提供)
  2. CMAKE_LANG_COMPILER(如果提供)
  3. 环境变量 CCCXX(如果提供)
  4. 然后它搜索路径环境变量以查看是否可以找到任何内容。

我想在你的情况下,以上都没有奏效。所以设置 CC 和 CXX 就解决了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?