FabioBatSilva / ArduinoFake

Arduino mocking made easy
https://platformio.org/lib/show/1689/ArduinoFake
MIT License
102 stars 47 forks source link

How do I compile this project with CMake? #49

Open enigmatichaptics opened 1 year ago

enigmatichaptics commented 1 year ago

Hi there. I'm new to Arduino and trying to use your mocking library as part of a suite of gtest framework tests.

My CMakeLists.txt file looks like this:

cmake_minimum_required(VERSION 3.14) # version can be different
project(TestProject) #name of your project
# Set compilation flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=1")
include(CTest)
include(FetchContent)
# Set a flag indicating whether building with CMake
set(BUILD_WITH_CMAKE TRUE)
# Conditionally include the appropriate headers based on BUILD_WITH_CMAKE
if(BUILD_WITH_CMAKE)
  add_compile_definitions(BUILD_WITH_CMAKE)
  include_directories(${CMAKE_CURRENT_BINARY_DIR}/_deps/arduinofake-src/src)
endif()

enable_testing()
FetchContent_Declare(
  googletest
  # Specify the commit you depend on and update it regularly.
  URL https://github.com/google/googletest/archive/5376968f6948923e2411081fd9372e71a59d8e77.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# Fetch ArduinoFake library
FetchContent_Declare(
    ArduinoFake
    GIT_REPOSITORY https://github.com/FabioBatSilva/ArduinoFake.git
    GIT_TAG cf9c596b73ffe7e7cf39faeb550bbef8598edb98
)
FetchContent_MakeAvailable(ArduinoFake)

# Now simply link against gtest or gtest_main as needed. Eg
add_executable(mytests mytests.cpp)
target_link_libraries(mytests gtest)
add_test(NAME example_test COMMAND mytests)

I had no issues running make before adding the ArduinoFake stuff, but am now getting the error:

[100%] Linking CXX executable main-d.exe
lto-wrapper.exe: warning: using serial compilation of 2 LTRANS jobs
lto-wrapper.exe: note: see the '-flto' option documentation for more information
lto1.exe: error: two or more sections for .gnu.lto__ZN10SerialTest19test_extends_streamEv.4317.e9bafa65
(null):0: confused by earlier errors, bailing out
lto-wrapper.exe: fatal error: C:\msys64\mingw64\bin\g++.exe returned 1 exit status
compilation terminated.
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: error: lto-wrapper failed
collect2.exe: error: ld returned 1 exit status

I'm not sure what I'm doing wrong or if I'm using the library wrong. Would anyone be willing to help me out?