Gerardo Zinno
Gerardo Zinno

Reputation: 1712

C++ code using CMake compiles on MacOs with clang but raise syntax errors on Linux

I need my code to work both on Linux and MacOs.

Here is the CMakeLists.txt file I'm using to generate the Makefiles.

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()

On MacOs the following steps work and I get the final executables:

~/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

After this compilation process I have my executables, no warnings or errors are raised.

Meanwhile if I try to compile this same code on linux Ubuntu, using the same commands:

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

I start getting errors like these ones.

For each for loop I get this error:

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)

saying that '=' is expected before '{', and the following error that I've never encountered:

/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)
      |                     ^

saying use of local variable with automatic storage from containing function.

How is it possible that on MacOs everything works well while on linux I get there errors? How can I solve them? I could swear that the code used to work fine on linux, I think it started to not compile after I included the

if (NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU")
        add_compile_options(-Wno-error=unused-command-line-argument)
    endif()

in the CMakeLists.txt, but now even if I comment that line out the code doesn't work.

The compilers used are displayed in the first line of the cmake's output.

I also tried the code on another linux machine and got the same errors.

Upvotes: 1

Views: 371

Answers (1)

Gerardo Zinno
Gerardo Zinno

Reputation: 1712

As pointed out by Tsyvarev in the comment, the problem was that my OpenMp on Linux didn't support the braces initialiser for the for iterators.

Upvotes: 1

Related Questions