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)