对于芯片ATSAMA5D35芯片来说,首先需要在你的linux虚拟机上安装交叉编译工具链arm-linux-gnueabihf- 使用的命令为sudo apt-get install arm-linux-gnueabihf安装完工具链之后检查是否安装完成,利用命令:arm-linux-gnueabihf-gcc -v查看对应的版本号。
接着是编译多线程,这时候我编译的错误是:
/media/sf_winshare/VMAX6000/app/thread/Vtelapp.c:45: undefined reference to `pthread_create'
obj/pthread/src/vtel_thr.o: In function `VTEL_thrCreate':
/media/sf_winshare/VMAX6000/app/thread/pthread/src/vtel_thr.c:18: undefined reference to `pthread_attr_setstacksize'
/media/sf_winshare/VMAX6000/app/thread/pthread/src/vtel_thr.c:37: undefined reference to `pthread_create'
obj/pthread/src/vtel_thr.o: In function `VTEL_thrJoin':
/media/sf_winshare/VMAX6000/app/thread/pthread/src/vtel_thr.c:55: undefined reference to `pthread_join'
obj/pthread/src/vtel_thr.o: In function `VTEL_thrDelete':
/media/sf_winshare/VMAX6000/app/thread/pthread/src/vtel_thr.c:66: undefined reference to `pthread_cancel'
很明显是找不到线程的相关接口,是在编译的时候缺少-pthread,但是在我的makefile中利用命令arm-none-linux-gnueabi-gcc 可以编译完成,并且在里面已经添加了-pthread,所以我怀疑是缺少对应的线程库,但是找度娘问了好多,都说和工具链没有关系,紧接着我就重新自己单独写了一个线程,然后用那个交叉编译工具链编译,没有问题,最终经过排查还是发现我的Makefile有问题,这里付下我的正确的Makefile文件:(注:标红的是我添加的,添加完之后运行成功);
TARGET = main
#CROSS_COMPILE = arm-none-linux-gnueabi-
CROSS_COMPILE = arm-linux-gnueabihf-
OBJ_DIR = obj
CC_FLAGS = -g -w -std=gnu99 -DLINUX
SOURCES = $(wildcard *.c)
SOURCES += $(wildcard pthread/src/*.c)
SOURCES += $(wildcard ipc/*.c)
INCPATH = -I./
INCPATH += -Ipthread/inc/
INCPATH += -Iipc/
OBJFILES = $(addprefix $(OBJ_DIR)/,$(SOURCES:%.c=%.o))
GNU_C_FLAGS = $(CC_FLAGS) $(INCPATH)
GNU_LD_FLAGS = -lpthread -lm
COMPILE.c = $(CROSS_COMPILE)gcc $(GNU_C_FLAGS) -c
LINK.c = $(CROSS_COMPILE)g++ $(GNU_LD_FLAGS)
.PHONY: clean $(TARGET)
all: $(TARGET)
$(TARGET): $(OBJFILES)
@echo Linking $@ from $^..
$(LINK.c) -Wl,--rpath=./ -o $(OBJ_DIR)/$@ $^ -lpthread
$(OBJFILES): $(OBJ_DIR)/%.o:%.c
@echo Compiling $@ from $<..
@mkdir -p $(dir $@)
$(COMPILE.c) -o $@ $<
clean:
@echo Removing generated files..
rm -rf $(OBJFILES) *~ *.d .dep
rm $(OBJ_DIR)/main