Jade Dungeon

交叉编译Raspberry Pi 上的程序

安装工具

## Install package dependencies
$ sudo apt-get install build-essential autoconf libtool cmake pkg-config git python-dev swig3.0 libpcre3-dev nodejs-dev

## For ARM 64bit toolchain
$ sudo apt-get install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu

## For armv7l toolchain
$ sudo apt-get install gcc-arm-linux-gnueabihf g++-arm-linux-gnueabihf

## For armv6 toolchain
$ sudo apt-get install gcc-arm-linux-gnueabi g++-arm-linux-gnueabi

确定处理器的arm版本:

$ uname -a
Linux raspberrypi49772a 5.4.51-v7l+ #1333 SMP Mon Aug 10 16:51:40 BST 2020 armv7l GNU/Linux

交叉编译C代码:

#include "stdio.h"

int main(void) {
  printf("Hello world!\n");
  return 0;
}
arm-linux-gnueabihf-gcc hello.c -o hello_for_armv7l

交叉编译C++代码:

#include "iostream"
 
using namespace std;
 
int main(int argc, char *argv[]) {
    cout << "Hello world!" << endl;
    return 0;
}
arm-linux-gnueabihf-g++ hello.cc -o hello_cc_armv7l_linux

makefile

例子:study/make-arm-arch

void print_hello();
int factorial(int n);
#include "functions.h"

int factorial(int n){
	if (n!=1) return n * factorial(n-1);
	else return 1;
}
#include <iostream>

void print_hello() {
	std::cout << "hello world" << std::endl;
}
# include <iostream>
# include "functions.h"

int main() {
	print_hello();
	std::cout << "this is main" << std::endl;
	std::cout << "The factorial of 5 is " << factorial(5) << std::endl;
	return 0;
}
# CC = g++                        # X86_64
# CC = arm-linux-gnueabi-g++      # armv7 toolchain
CC = arm-linux-gnueabihf-g++    # armv6 toolchain
# CC = aarch64-linux-gnu-g++        # arm 64bit toolchain

SRC_DIR = src
OUT_DIR = out
OBJS = $(patsubst $(SRC_DIR)/%.cpp, $(OUT_DIR)/%.o, $(wildcard $(SRC_DIR)/*.cpp))

CFLAGS = -c -Wall
LFLAGS = -Wall
CYGLIB = -static-libgcc -static-libstdc++

all: $(OUT_DIR)/hello
$(OUT_DIR)/hello: $(OBJS)
        $(CC) $(LFLAGS) $(CYGLIB) $^ -o $@
$(OBJS): $(OUT_DIR)/%.o : $(SRC_DIR)/%.cpp
        $(CC) $(CFLAGS) $(CYGLIB) $< -o $@

clean:
        rm -rf $(OUT_DIR)/*.o $(OUT_DIR)/hello