下面是一个糅合多线程和多文件的示例
emc-test.c
#include <stdio.h> #include <pthread.h> #include "charge-test.h" #include "rs485-test.h" pthread_mutex_t mutex; pthread_cond_t cond; int main() { pthread_t thread1, thread2; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); pthread_create(&thread1, NULL, (void*)thread1_rs485, NULL); pthread_create(&thread2, NULL, (void*)thread2_charge, NULL); do{ pthread_cond_signal(&cond); }while(1); sleep(20); pthread_exit(0); return 0; }
emc-test.h
#ifndef __EMC_TEST_H #define __EMC_TEST_H #endif
charge-test.c
#include <stdio.h> #include <pthread.h> #include "charge-test.h" void *thread2_charge(void *arg) { }
charge-test.h
#ifndef __CHARGE_TEST_H #define __CHARGE_TEST_H void *thread2_charge(void *arg); #endif
rs485-test.c
#include <stdio.h> #include <pthread.h> #include "rs485-test.h" void *thread1_rs485(void *arg) { }
rs485-test.h
#ifndef __RS485_H #define __RS485_H void *thread1_rs485(void *arg); #endif
Makefile
CC=gcc #arm-linux-gnueabihf-gcc CFLAGS=-Wall TARGET=emc-test SRCS=$(wildcard *.c) OBJS=$(SRCS:.c=.o) #$(shell echo $(OBJS) > a.txt) ${TARGET}:${OBJS} $(CC) $(OBJS) -pthread -o $@ clean: rm -rf $(TARGET) $(OBJS) *.o *~ %.o:%.c %.h $(CC) -c $< -o $@