Hey all, I made an example CMake project for calling Nim code from a C/C++ project, and I'd like to get some feedback. I plan on using this for an existing C++ project of mine so I wanted to get some practice and see if I could make the process easier.
Repo is here - https://github.com/stoneface86/example-nim-cmake
Let me know what you think. I have only tested it with windows (MSVC) so far, I'll get CI set up momentarily.
About the other way, ie. CMake project for calling C/C++ code from a Nim project ?
actually I ask explanation if it is what known as FFI uses ?
Check the backend intergration page in the manual. It's much easier doing the opposite IMO (Nim calling C/C++) and you generally do not need CMake for this as you can tell Nim where to link and what headers to use in your Nim code. Depends on what you're wrapping.
I'd do something like this if using cmake:
add_custom_command(
OUTPUT "${NIM_EXE_PATH}"
COMMAND "${NIMBLE}" c --out:"${NIM_EXE_PATH}" "${NIM_PROJECT}"
DEPENDS "${NIM_PROJECT}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)
add_custom_target(
nimbuild
DEPENDS "${NIM_EXE_PATH}"
COMMENT "Building Nim project"
)
add_executable(project IMPORTED GLOBAL)
set_target_properties(project PROPERTIES IMPORTED_LOCATION "${NIM_EXE_PATH}")
add_dependencies(project nimbuild)
A lot was omitted for this example but you would also pass your C/C++ target's headers, libs, defines to the nim compiler, which you can do via cmake generator expressions.