我有一项新任务来研究开发 C++ 跨平台 (Mac/Win) 实用程序的方式以满足我们的内部需求。
我已经使用 C#、Java、Managed C++ 等不同的“粉红色”语言开发了 7 年。但是在这个任务中,要求是支持 Mac 和在 Mac 上运行的 .NET ,真的很痛苦(从其他使用过这个的人那里知道这一点)。
所以我开始考虑 C++ 是否可以使用 C++ 进行跨平台开发。该应用程序将不包含任何 GUI,但将包含大量系统 API 调用和大量业务逻辑分析。
是否有一些库允许实现这种任务?有可能做吗?
我有一个包含 3 个项目的 C++ VS2013 解决方案,使用 OpenCV 2.4.10、easylogging++ 和 Dlib 库。问题是我们的客户端现在正在迁移到 Linux(Ubuntu 服务器)。我已经研究了 Cygwin 和 MinGW 以及这里关于 stackoverflow 的一些问题,但我仍然不知道应该从哪里开始。我需要尽快完成此操作,因此最好使用更简单的解决方案。我非常感谢您能提供的任何帮助。
我在我的 arch linux 发行版上安装了 mingw,以便交叉编译 Windows 应用程序。一切似乎都工作正常,除了当我包含 Winsock2 时:
x86_64-w64-mingw32-gcc -c -Wall -o tcp.o tcp.c
tcp.c:14:24: fatal error: Winsock2.h: No such file or directory
#include <Winsock2.h>
Run Code Online (Sandbox Code Playgroud)
我错过了什么包裹?
Cmake 是我正在尝试使用 cmake 在 Mac 上交叉编译树莓派 3+ 来生成 makefile。我的 CMakeLists.txt:
cmake_minimum_required (VERSION 3.11.0)
project(decatrack)
# setup cross toolchain for RPi 3
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(TOOLCHAIN_DIR /Volumes/xtool-build-env/armv8-rpi3-linux-gnueabihf)
set(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/armv8-rpi3-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/armv8-rpi3-linux-gnueabihf-g++)
set(CMAKE_SYSROOT ${TOOLCHAIN_DIR}/armv8-rpi3-linux-gnueabihf/sysroot)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
unset(CMAKE_OSX_DEPLOYMENT_TARGET
# c++ standerd
# force cc++03 standard
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -mcpu=cortex-a53 -mfloat-abi=hard -mfpu=neon-fp-armv8 -mneon-for-64bits -ggdb -O0 -ffunction-sections -fdata-sections -v -shared-libgcc")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
message(STATUS "Using C++11")
include_directories(
${TOOLCHAIN_DIR}/armv8-rpi3-linux-gnueabihf/sysroot/usr/include
${TOOLCHAIN_DIR}/armv8-rpi3-linux-gnueabihf/include/c++/6.3.0
${TOOLCHAIN_DIR}/armv8-rpi3-linux-gnueabihf/include/c++/6.3.0/backward
${TOOLCHAIN_DIR}/lib/gcc/armv8-rpi3-linux-gnueabihf/6.3.0/include
${TOOLCHAIN_DIR}/lib/gcc/armv8-rpi3-linux-gnueabihf/6.3.0/include-fixed
${TOOLCHAIN_DIR}/armv8-rpi3-linux-gnueabihf/include
)
target_link_libraries (
${TOOLCHAIN_DIR}/armv8-rpi3-linux-gnueabihf/sysroot/lib/libpigpio.so …Run Code Online (Sandbox Code Playgroud) 我有一个 Rust 库,它为FFI. 我假设我必须设置crate-type为cdylib- 因为我想从 Ruby 和 PHP 调用这些函数(通过ffiruby gem调用)。但是我无法将它从 OSX 交叉编译到 Linux。我试图遵循一些使用的教程musl libc- 这是针对静态库的,但我还没有找到其他任何东西。
所以链接器是这样定义的:
# .cargo/config
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
Run Code Online (Sandbox Code Playgroud)
我试图用以下方法编译它:
cargo build --release --target x86_64-unknown-linux-musl
Run Code Online (Sandbox Code Playgroud)
但是有直接的错误:
error: cannot produce cdylib for `my-crate-name` as the target `x86_64-unknown-linux-musl` does not support these crate types
Run Code Online (Sandbox Code Playgroud)
我的问题是:什么目标/链接器对可用于交叉编译cdylib?为什么 musl 不支持这些 crate 类型?甚至有可能吗?
我正在使用 TI-SDK 交叉编译用于 ARM 的 python 3.7。
我已经在 ubuntu 上下载了源代码并按照以下说明进行操作
当我运行时make,出现以下错误。您能告诉我如何解决链接器错误吗?
*ar rc libpython3.5m.a Modules/config.o Modules/getpath.o Modules/main.o Modules/gcmodule.o
ar rc libpython3.5m.a Modules/_threadmodule.o Modules/signalmodule.o Modules/posixmodule.o Modules/errnomodule.o Modules/pwdmodule.o Modules/_sre.o Modules/_codecsmodule.o Modules/_weakref.o Modules/_functoolsmodule.o Modules/_operator.o Modules/_collectionsmodule.o Modules/itertoolsmodule.o Modules/atexitmodule.o Modules/_stat.o Modules/timemodule.o Modules/_localemodule.o Modules/_iomodule.o Modules/iobase.o Modules/fileio.o Modules/bytesio.o Modules/bufferedio.o Modules/textio.o Modules/stringio.o Modules/zipimport.o Modules/faulthandler.o Modules/_tracemalloc.o Modules/hashtable.o Modules/symtablemodule.o Modules/xxsubtype.o
ranlib libpython3.5m.a
gcc -pthread -Xlinker -export-dynamic -o python Programs/python.o libpython3.5m.a -lpthread -ldl -lutil -lm
libpython3.5m.a(fileutils.o): In function `set_inheritable':
/home/pcadmin/work/OTIS/Python-3.5.9/Python/fileutils.c:892: undefined reference to …Run Code Online (Sandbox Code Playgroud) 这个问题与go-sqlite3 交叉编译 OSX 到 linux 的编译器参数相同,但由于这个问题没有答案,我会再次尝试询问。我有一个使用github.com/mattn/go-sqlite3 SQLite3 驱动程序的 Go 包,类似于以下内容:
package main
import (
_ "github.com/mattn/go-sqlite3"
)
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试在启用了 CGO 的情况下为 Linux AMD64 编译此文件(按照 SQLite3 的要求),我会收到以下错误:
> env GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build
# github.com/mattn/go-sqlite3
sqlite3-binding.c:33123:42: error: use of undeclared identifier 'pread64'
sqlite3-binding.c:33141:42: error: use of undeclared identifier 'pwrite64'
sqlite3-binding.c:33274:22: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'
sqlite3-binding.c:33283:22: error: invalid application of 'sizeof' to an incomplete type 'struct unix_syscall []'
sqlite3-binding.c:33310:20: error: invalid application …Run Code Online (Sandbox Code Playgroud) 我正在准备演示 Qemu 的用户模式(qemu-user 包)qemu-arm。为此,我使用了一个简单的 hello world C 程序hello.c:
#include <stdio.h>
int main()
{
printf("Oi, Qemu!\nPrograma C aqui!\n");
}
Run Code Online (Sandbox Code Playgroud)
为了交叉编译它(静态链接),我使用了以下的交叉工具链gcc-arm-linux-gnueabihf:
$ arm-linux-gnueabihf-gcc --version
arm-linux-gnueabihf-gcc (Ubuntu 9.3.0-10ubuntu1) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ arm-linux-gnueabihf-gcc hello.c -o hello_c_static -static
Run Code Online (Sandbox Code Playgroud)
输出在 qemu-arm、Beaglebone Black和 PC 上运行。
这怎么可能?!
关于编译后的可执行文件:
file hello_c_static
hello_c_static: …Run Code Online (Sandbox Code Playgroud) 我想得到一个 windows-DLL,但我想在 Ubuntu-Linux 下编译它。
构建一个 Executable 很简单:env GOOS=windows GOARCH=386 go build wrapper.go生成一个wrapper.exe, 行为如预期。
但是构建一个 DLL 会env GOOS=windows GOARCH=386 go build -buildmode=c-shared wrapper.go导致错误:
running gcc failed: exit status 1
gcc: error: unrecognized command line option ‘-mconsole’; did you mean ‘--compile’?
Run Code Online (Sandbox Code Playgroud)
我不想go在 windows 下安装和运行,因为我的完整工具链是在 Ubuntu 下运行的
go version go1.15.6 linux/amd64
我很好奇静态链接的 C 可执行文件如何在不同的环境中工作。假设我们编译我们的 C 代码以针对 x86 MacO,并且我们静态地包含它在可执行文件中使用的所有内容(print、strlen)。如果我们包含它需要的每个库,什么真正阻止这个可执行文件在 Windows 操作系统中运行?我知道文件格式可能不同并且会中断,但除此之外,这在技术上是否能够运行?