1

I work on the native part of my C# lib written in C. This part provides native socket API for main platforms win/mac/linux. I'm already building sources independently on each platform using CMake.

My current goal builds lib for these platforms at my dev machine (MacOS).

Platforms list:

# 64 bit builds

- linux64
- osx
- win64

# 32 bit builds

- linux32
- win32

After reading CMake docs about cross-compiling, and search some examples on GitHub I still have trouble with this task.

Docs: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html

GitHub search: https://github.com/search?q=CMake+cross+compiling&type=Repositories

Could anyone explain a detailed step-by-step algorithm of what could I do to reach this goal? A Simple "Hello World!" example with shared lib would be very helpful or link on the existing sample/project.

Thanks.

Update:

Project structure:

build/
cmake/
  x64-linux.cmake
  x86_64-osx.cmake
src/
  native_socket.c
  native_socket.h


CMakeLists.txt
build_native.sh

CMakeLists.txt:

cmake_minimum_required(VERSION 3.20.1)

project(native_socket LANGUAGES C)

add_library(native_socket SHARED src/native_socket.c)
set_target_properties(native_socket PROPERTIES PREFIX "")

build_native.sh:

cmake -H. -B build/linux64 -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=cmake/x64-linux.cmake
cmake --build ./build/linux64 -v

cmake -H. -B build/osx -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=cmake/x86_64-osx.cmake
cmake --build ./build/osx

x86_64-osx.cmake:

set(CMAKE_SYSTEM_NAME Darwin)
set(CMAKE_C_COMPILER gcc)

x64-linux.cmake:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)

set(CMAKE_C_COMPILER gcc)
set(CMAKE_CROSSCOMPILING TRUE)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

After running build script, I see a linker error in the terminal:

% ./build_native.sh
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/rdcm/dev/projects/PInvoke/PInvoke/build/linux64
/Applications/CMake.app/Contents/bin/cmake -S/Users/rdcm/dev/projects/PInvoke/PInvoke -B/Users/rdcm/dev/projects/PInvoke/PInvoke/build/linux64 --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CMake.app/Contents/bin/cmake -E cmake_progress_start /Users/rdcm/dev/projects/PInvoke/PInvoke/build/linux64/CMakeFiles /Users/rdcm/dev/projects/PInvoke/PInvoke/build/linux64//CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f CMakeFiles/native_socket.dir/build.make CMakeFiles/native_socket.dir/depend
cd /Users/rdcm/dev/projects/PInvoke/PInvoke/build/linux64 && /Applications/CMake.app/Contents/bin/cmake -E cmake_depends "Unix Makefiles" /Users/rdcm/dev/projects/PInvoke/PInvoke /Users/rdcm/dev/projects/PInvoke/PInvoke /Users/rdcm/dev/projects/PInvoke/PInvoke/build/linux64 /Users/rdcm/dev/projects/PInvoke/PInvoke/build/linux64 /Users/rdcm/dev/projects/PInvoke/PInvoke/build/linux64/CMakeFiles/native_socket.dir/DependInfo.cmake --color=
Dependencies file "CMakeFiles/native_socket.dir/src/native_socket.c.o.d" is newer than depends file "/Users/rdcm/dev/projects/PInvoke/PInvoke/build/linux64/CMakeFiles/native_socket.dir/compiler_depend.internal".
Consolidate compiler generated dependencies of target native_socket
/Applications/Xcode.app/Contents/Developer/usr/bin/make  -f CMakeFiles/native_socket.dir/build.make CMakeFiles/native_socket.dir/build
[ 50%] Linking C shared library native_socket.so
/Applications/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/native_socket.dir/link.txt --verbose=1
/usr/bin/gcc -fPIC -O3 -DNDEBUG -shared -Wl,-soname,native_socket.so -o native_socket.so CMakeFiles/native_socket.dir/src/native_socket.c.o 
ld: unknown option: -soname
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [native_socket.so] Error 1
make[1]: *** [CMakeFiles/native_socket.dir/all] Error 2
make: *** [all] Error 2

Questions:

  • Should I additional install dev tools on my dev machine for building .so/.dll lib for Linux/Win?
  • How should I change CMake config files for the successful build?
  • A real simple example that works will be very helpful. I think no matter in which environment Linux/MacOS because I have plans to automate this process.
rdcm
  • 11
  • 4
  • "I still have trouble with this task." - What exact trouble do you have? Please, show (describe in the question post) **what have you tried** and the error message you got. – Tsyvarev Apr 23 '21 at 22:18
  • Question updated with some additional info about project structure and current build process. I think main problem for me in miss understanding preparing host to cross-compilation and correct cmake configuration. – rdcm Apr 24 '21 at 10:01
  • 1
    I wouldn't expect `gcc` installed **natively** for MacOS to be able to cross-compile for Linux. You need real cross-compiler. See e.g. [that question](https://stackoverflow.com/questions/1775267/how-to-cross-compile-from-mac-os-x-to-linux-x86). As for cross-compiling on MacOS for Windows, this should be a **separate question**: On Stack Overflow we expect the question to be focused on a single problem. – Tsyvarev Apr 24 '21 at 14:39

0 Answers0