简体   繁体   English

如何避免每次编译时都必须键入 -std=c++11

[英]How do I avoid having to type -std=c++11 each time when compiling

SOLUTION : I used the solution by Sander De Dycker and just updated my gcc version, which worked perfectly.解决方案:我使用了Sander De Dycker的解决方案,刚刚更新了我的 gcc 版本,效果很好。

First, I want to point out that I don't know anything about CMake or shell scripts.首先,我想指出我对 CMake 或 shell 脚本一无所知。

What is the best way to avoid having to type -std=c++11 each time when compiling a C++ file?在编译 C++ 文件时避免每次都输入-std=c++11的最佳方法是什么

(eg g++ -std=c++11 file.c). (例如 g++ -std=c++11 file.c)。

  • I found a solution online, but it didn't seem to work or I didn't know how to use it properly:我在网上找到了一个解决方案,但它似乎不起作用或我不知道如何正确使用它:

    https://github.com/openalpr/openalpr/issues/528 https://github.com/openalpr/openalpr/issues/528

    Put this line :把这一行:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")设置(CMAKE_CXX_FLAGS“${CMAKE_CXX_FLAGS} -std=c++11”)

    after the first line of CMakeLists.txt located in src directory在位于 src 目录中的CMakeLists.txt的第一行之后

    This CMakeLists.txt file didn't exist, so I just created it.这个CMakeLists.txt文件不存在,所以我只是创建了它。 This might've been a first mistake.这可能是第一个错误。 Moreover, my assumption was that then I could just type g++ file.c , but maybe I have to type something else instead?此外,我的假设是我可以只输入g++ file.c ,但也许我必须输入其他内容?

  • My supervisor told me that the most efficient way was to use a shell script , which I am not familiar with.我的主管告诉我,最有效的方法是使用我不熟悉的shell script Any suggestions for this method?对这种方法有什么建议吗?

CMakeLists.txt is the rule file build your project, gcc is one compiler. CMakeLists.txt 是构建项目的规则文件,gcc 是一种编译器。

  1. create a CMakeLists.txt in your dir.在您的目录中创建一个 CMakeLists.txt。
cmake_minimum_required(VERSION 3.0)
project(project_name)
# use c++ 11
set(CMAKE_CXX_STANDARD  11)
add_executable(func file.cpp)
  1. create folder named build , and cd build创建名为build文件夹,然后cd build
  2. build it建造它
cmake ..
make
  1. finaly, you'll get execute file named func .最后,您将获得名为func执行文件。

you should learn about CMake or Make https://cmake.org/cmake-tutorial/你应该了解 CMake 或 Make https://cmake.org/cmake-tutorial/

An even simpler option is stick your defaults in a Makefile :一个更简单的选择是将您的默认设置保存在Makefile

CPPFLAGS += -std=c++11 -Wall -Wextra -pedantic -Wshadow

Then you can just call make file and Make will compile file.cpp into an executable file for you.然后你可以调用make file ,Make 会为你编译file.cpp为可执行file

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM