首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用C++使用CMake的代码在MacOs上使用clang编译,但在Linux上会引发语法错误。

使用C++使用CMake的代码在MacOs上使用clang编译,但在Linux上会引发语法错误。
EN

Stack Overflow用户
提问于 2021-12-10 19:57:22
回答 1查看 172关注 0票数 1

我需要我的代码同时在Linux和MacOs上工作。

下面是我用来生成Makefile的CMakeLists.txt文件。

代码语言:javascript
复制
cmake_minimum_required(VERSION 3.10)

# set the project name and version
project(PCATests    VERSION 0.1
                                    DESCRIPTION "tests of the framework for building Cellular Automata"
                                    LANGUAGES CXX)

# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package(OpenMP REQUIRED)

if (${OPENMP_FOUND})
    include_directories(${INCLUDE_DIRS})
endif()

include_directories(../../include ../ext)
link_directories(../../build)

# compile options
if (MSVC)
    # warning level 4 and all warnings as errors
    add_compile_options(/W4 /WX)
    # if the compiler supports OpenMP, use the right flags
    if (${OPENMP_FOUND})
        add_compile_options(${OpenMP_CXX_FLAGS})
    endif()
else()
    # lots of warnings and all warnings as errors
    add_compile_options(-Wall -Wextra -pedantic -Werror)
    if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU")
        add_compile_options(-Wno-error=unused-command-line-argument)
    endif()
    # optimizations and debug informations
    add_compile_options(-g -O3)
    # if the compiler supports OpenMP, use the right flags
    if (${OPENMP_FOUND})
        add_compile_options(${OpenMP_CXX_FLAGS})
    endif()

endif()

set(unit_test_targets
    test_sequential_all
    test_operators
    test_library_imports
    test_sequential_automaton
    test_utilities
    test_sequential_leaks_valgrind
    test_omp_automaton
)

foreach(TARGET ${unit_test_targets})
    add_executable(${TARGET} ${TARGET}.cpp)
    target_link_libraries(${TARGET} parallelcellularautomata)
endforeach()

在MacOs上,以下步骤起作用,我得到了最终的可执行文件:

代码语言:javascript
复制
~/repos/parallel-cellular-automata/tests/unit/build$ pwd
/Users/gerardozinno/repos/parallel-cellular-automata/tests/unit/build
~/repos/parallel-cellular-automata/tests/unit/build$ cmake ..
-- The CXX compiler identification is AppleClang 11.0.0.11000033
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenMP_CXX: -Xclang -fopenmp (found version "3.1")
-- Found OpenMP: TRUE (found version "3.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/gerardozinno/repos/parallel-cellular-automata/tests/unit/build
~/repos/parallel-cellular-automata/tests/unit/build$ make
Scanning dependencies of target test_omp_automaton
...
Scanning dependencies of target test_sequential_automaton
[  7%] Building CXX object CMakeFiles/test_sequential_leaks_valgrind.dir/test_sequential_leaks_valgrind.cpp.o
...

[100%] Built target test_sequential_all

在此编译过程之后,我的可执行文件将不会引发警告或错误。

同时,如果我试图在linux Ubuntu上编译相同的代码,使用相同的命令:

代码语言:javascript
复制
gerardo@newton:~/repos/parallel-cellular-automata/tests/unit/build$ pwd
/home/gerardo/repos/parallel-cellular-automata/tests/unit/build
gerardo@newton:~/repos/parallel-cellular-automata/tests/unit/build$ cmake ..
-- The CXX compiler identification is GNU 9.3.0
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenMP_CXX: -fopenmp (found version "4.5")
-- Found OpenMP: TRUE (found version "4.5")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/gerardo/repos/parallel-cellular-automata/tests/unit/build
gerardo@newton:~/repos/parallel-cellular-automata/tests/unit/build$ make
Scanning dependencies of target test_omp_automaton
[  7%] Building CXX object CMakeFiles/test_omp_automaton.dir/test_omp_automaton.cpp.o

我开始犯像这样的错误。

对于每个For循环,我得到以下错误:

代码语言:javascript
复制
In file included from /home/gerardo/repos/parallel-cellular-automata/tests/unit/../../include/cellular_automata.hpp:6,
                 from /home/gerardo/repos/parallel-cellular-automata/tests/unit/test_omp_automaton.cpp:7:
/home/gerardo/repos/parallel-cellular-automata/tests/unit/../../include/omp_automaton.hpp: In member function ‘virtual void ca::omp::CellularAutomaton<T>::sim
ulate(unsigned int)’:
/home/gerardo/repos/parallel-cellular-automata/tests/unit/../../include/omp_automaton.hpp:93:22: error: expected ‘=’ before ‘{’ token
   93 |         for (size_t i{0}; i < rows; ++i)

在“{”之前表示“=”,下面是我从未遇到过的错误:

代码语言:javascript
复制
/home/gerardo/repos/parallel-cellular-automata/tests/unit/../../include/omp_automaton.hpp:93:9: error: use of local variable with automatic storage from conta
ining function
   93 |         for (size_t i{0}; i < rows; ++i)
      |         ^~~
/home/gerardo/repos/parallel-cellular-automata/tests/unit/../../include/omp_automaton.hpp:93:21: note: ‘size_t i’ declared here
   93 |         for (size_t i{0}; i < rows; ++i)
      |                     ^

说明使用局部变量自动存储从包含函数。

在MacOs上,怎么可能一切都运行得很好,而在linux上,我却出现了错误?我该怎么解决呢?我可以发誓,以前的代码在linux上运行良好,我认为它在包含了

代码语言:javascript
复制
if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU")
        add_compile_options(-Wno-error=unused-command-line-argument)
    endif()

在CMakeLists.txt中,但是现在即使我注释了这一行代码也不起作用。

所使用的编译器显示在cmake输出的第一行。

我还在另一台linux机器上尝试了代码,并得到了相同的错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-14 17:35:29

正如Tsyvarev在评论中指出的那样,问题是我在Linux上的OpenMp不支持for迭代器的大括号初始化。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70309766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档