0

I have been trying to compile a small test on linux using cmake and mingw. However, the criterion.h library does not get linked when I cross-compile (regular compiling works fine). The toolchain file specifies mingw as the compiler. But I do not know how to make it link the criterion.h file. When I do cmake --build ., I get the error test_stock_api.c:4:10: fatal error: criterion/criterion.h: No such file or directory.

Test code

// test_stock_api.c

#include <criterion/criterion.h>
#include <string.h>
#include "../src/stock.h"

Test(u_stock_api, set_tiingo_api) {
    set_tiingo_api("new_api_key");
    cr_assert(strcmp(TIINGO_API_KEY, "new_api_key") == 0, \
    "Tiingo api key could not be set.");
}

CMakeLists file

# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(stocklib)
add_library(stock STATIC src/stock.c)

include(CTest)

#set(CMAKE_CROSSCOMPILING true)
set(CMAKE_EXE_LINKER_FLAGS "-static" "-rpath-link")

# single test setup
add_executable(test_stock_api tests/test_stock_api.c)
target_link_libraries(test_stock_api -lcriterion)
target_link_libraries(test_stock_api stock)

add_test(
    NAME test_stock_api
    COMMAND $<TARGET_FILE:test_stock_api>
)

Toolchain file

# cmake cross-compiling toolchain file
set(CMAKE_SYSTEM_NAME Windows)

# which compilers to use for C and C++
set(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++)
set(CMAKE_RC_COMPILER x86_64-w64-mingw32-windres)

# here is the target environment located
set(CMAKE_FIND_ROOT_PATH  /usr/x86_64-w64-mingw32 /usr/include /usr/local)

# adjust the default behaviour of the FIND_XXX() commands:
# search headers and libraries in the target environment, search
# programs in the host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
UndoingTech
  • 709
  • 3
  • 16
  • You need to specify directory where `criterion/criterion.h` file is located as **include** one. This can be done with `include_directories` or `target_include_directories` commands. See e.g. [that question](https://stackoverflow.com/questions/13703647/how-to-properly-add-include-directories-with-cmake). – Tsyvarev Sep 09 '22 at 23:11
  • @Tsyvarev Thank you! I was able to include criterion using `target_link_libraries`. Though now I get an error for `bits/libc-header-start.h: No such file or directory`. If I do include the bits, I get multiple conflicting type errors. I am not sure I should include this because of [this answer](https://stackoverflow.com/a/30421747/4663968). But I do not know how to "install" more libraries to mingw other than copying from my native system. – UndoingTech Sep 10 '22 at 14:01
  • oops. I meant I used `target_include_directories` not `target_link_libraries`. – UndoingTech Sep 10 '22 at 23:49

0 Answers0