Initial commit
This commit is contained in:
11
.gitignore
vendored
Normal file
11
.gitignore
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
bin
|
||||
bin2
|
||||
compile_commands.json
|
||||
.clangd
|
||||
temp.*
|
||||
.vs
|
||||
.cache
|
||||
build*/
|
||||
.idea/
|
||||
cmake-build*/
|
||||
CMakeLists.txt.user
|
||||
9
CMakeLists.txt
Normal file
9
CMakeLists.txt
Normal file
@@ -0,0 +1,9 @@
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
project(cmake-zig)
|
||||
|
||||
add_library(mylib-static STATIC src/mylib.cpp)
|
||||
add_library(mylib-shared SHARED src/mylib.cpp)
|
||||
|
||||
add_executable(myexe src/myexe.cpp)
|
||||
target_link_libraries(myexe PRIVATE mylib-static)
|
||||
11
README.md
Normal file
11
README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# zig-cross
|
||||
|
||||
Example of using [zig](https://ziglang.org) as a CMake Toolchain for cross compiling.
|
||||
|
||||
Reference: https://zig.news/kristoff/cross-compile-a-c-c-project-with-zig-3599
|
||||
|
||||
## Building
|
||||
|
||||
- [Install zig](https://ziglang.org/learn/getting-started/#installing-zig) in your PATH (`choco install zig` on Windows)
|
||||
- `cmake -B build-aarch64 -G Ninja -DCMAKE_TOOLCHAIN_FILE=cmake/zig-toolchain-aarch64.cmake`
|
||||
- `cmake --build build-arch64`
|
||||
2
cmake/zig-ar.cmd
Normal file
2
cmake/zig-ar.cmd
Normal file
@@ -0,0 +1,2 @@
|
||||
@echo off
|
||||
zig ar %*
|
||||
2
cmake/zig-ar.sh
Executable file
2
cmake/zig-ar.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
zig ar "$@"
|
||||
2
cmake/zig-ranlib.cmd
Normal file
2
cmake/zig-ranlib.cmd
Normal file
@@ -0,0 +1,2 @@
|
||||
@echo off
|
||||
zig ranlib %*
|
||||
2
cmake/zig-ranlib.sh
Executable file
2
cmake/zig-ranlib.sh
Executable file
@@ -0,0 +1,2 @@
|
||||
#!/bin/sh
|
||||
zig ranlib "$@"
|
||||
18
cmake/zig-toolchain-aarch64.cmake
Normal file
18
cmake/zig-toolchain-aarch64.cmake
Normal file
@@ -0,0 +1,18 @@
|
||||
if(CMAKE_GENERATOR MATCHES "Visual Studio")
|
||||
message(FATAL_ERROR "Visual Studio generator not supported, use: cmake -G Ninja")
|
||||
endif()
|
||||
set(CMAKE_SYSTEM_NAME "Linux")
|
||||
set(CMAKE_SYSTEM_VERSION 1)
|
||||
set(CMAKE_SYSTEM_PROCESSOR "aarch64")
|
||||
set(CMAKE_C_COMPILER "zig" cc -target aarch64-linux-gnu)
|
||||
set(CMAKE_CXX_COMPILER "zig" c++ -target aarch64-linux-gnu)
|
||||
|
||||
if(WIN32)
|
||||
set(SCRIPT_SUFFIX ".cmd")
|
||||
else()
|
||||
set(SCRIPT_SUFFIX ".sh")
|
||||
endif()
|
||||
|
||||
# This is working (thanks to Simon for finding this trick)
|
||||
set(CMAKE_AR "${CMAKE_CURRENT_LIST_DIR}/zig-ar${SCRIPT_SUFFIX}")
|
||||
set(CMAKE_RANLIB "${CMAKE_CURRENT_LIST_DIR}/zig-ranlib${SCRIPT_SUFFIX}")
|
||||
8
src/myexe.cpp
Normal file
8
src/myexe.cpp
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <cstdio>
|
||||
|
||||
#include "mylib.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
puts(mylib());
|
||||
}
|
||||
6
src/mylib.cpp
Normal file
6
src/mylib.cpp
Normal file
@@ -0,0 +1,6 @@
|
||||
#include "mylib.h"
|
||||
|
||||
const char* mylib()
|
||||
{
|
||||
return "hello from mylib";
|
||||
}
|
||||
10
src/mylib.h
Normal file
10
src/mylib.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef _WIN32
|
||||
#define MYLIB_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define MYLIB_EXPORT
|
||||
#endif // _WIN32
|
||||
|
||||
|
||||
extern "C" MYLIB_EXPORT const char* mylib();
|
||||
Reference in New Issue
Block a user