我需要我的代码在Linux和MacO上都能工作。
这是我用来生成Makefiles的CMakeLists.txt文件。
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()
在MacO上,以下步骤有效,我得到了最终的可执行文件:
~/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上使用相同的命令编译相同的代码:
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循环,我都会得到这个错误:
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)
说在"{"之前应为"=",以及以下我从未遇到过的错误:
/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上运行得很好,我认为在我包含之后,它就开始不编译了
if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU")
add_compile_options(-Wno-error=unused-command-line-argument)
endif()
在CMakeLists.txt中,但现在即使我注释了这一行,代码也不起作用。
所使用的编译器显示在cmake输出的第一行中。
我还在另一台linux机器上尝试了这些代码,结果也出现了同样的错误。
for
迭代器的大括号初始化程序。