上一篇一步一步写一个简单通用的makefile(二) 里面的makefile 实现对通用的代码进行编译,这一章我将会对上一次的makefile 进行进一步的优化.
优化后的makefile:
#Hellomake
#Magnum, 2014-10-20
# 指令编译器和选项
CC=gcc
CFLAGS=-Wall
# 需要链接库的库名,比如libm.a,就是-lm,需要去掉前面的lib和后面的.a
LIBS=-lm
# 设置默认搜索头文件的路径,优先是这个,然后是系统路径
IncludeDir = -I./include/
# 需要链接的库的路径
LinkDir = #-L
OBJ_DIR = ./obj
BIN_DIR = ./bin
#PROJECT_TOP_DIR 设置成pwd 或者"./"都行
PROJECT_TOP_DIR=$(shell pwd)#$(shell cd ../; pwd)
PROJECT_BIN_DIR=$(PROJECT_TOP_DIR)/bin
PROJECT_SRC_DIR=$(PROJECT_TOP_DIR)/src
PROJECT_LIB_DIR=$(PROJECT_TOP_DIR)/lib
PROJECT_OBJ_DIR=$(PROJECT_TOP_DIR)/objs
MKDIR := mkdir -p
# 目标文件
EXE_NAME=hellomake
TARGET=$(BIN_DIR)/$(EXE_NAME)
#源文件的文件类型
FILE_TYPE=c
src=$(wildcard $(PROJECT_SRC_DIR)/*.$(FILE_TYPE))
dir= $(notdir $(src))
PROJECT_OBJ= $(patsubst %.$(FILE_TYPE),%.o,$(dir) )
PROJECT_ALL_OBJS= $(addprefix $(PROJECT_OBJ_DIR)/, $(PROJECT_OBJ))
all: chdir $(TARGET)
@echo "magnum $(PROJECT_OBJ)"
@echo "magnum $(PROJECT_OBJ_DIR)"
@echo "magnum $(PROJECT_ALL_OBJS)"
$(TARGET): $(PROJECT_ALL_OBJS)
$(CC) -o $@ $^ $(LinkDir) $(LIBS)
chdir:
@if test ! -d $(PROJECT_OBJ_DIR) ;
then
mkdir $(PROJECT_OBJ_DIR) ;
fi
@if test ! -d $(PROJECT_BIN_DIR) ;
then
mkdir $(PROJECT_BIN_DIR) ;
fi
.PHONY : clean
clean:
-rm -rf $(PROJECT_BIN_DIR) $(PROJECT_OBJ_DIR)
$(PROJECT_OBJ_DIR)/%.o:$(PROJECT_SRC_DIR)/%.$(FILE_TYPE)
$(CC) $(CFLAGS) -o $@ -c $< $(IncludeDir)
这个优化后的makefile 对于一般需要写一个小的测试程序都有一定的通用性,如果需要对新的程序进行修改:
1. 编译类型C用gcc, c++用g++
2. 源文件的路径 PROJECT_SRC_DIR
3. 文件类型c 还是cpp
4. 还有这三个:
# 需要链接库的库名,比如libm.a,就是-lm,需要去掉前面的lib和后面的.a LIBS=-lm # 设置默认搜索头文件的路径,优先是这个,然后是系统路径 IncludeDir = -I./include/ # 需要链接的库的路径 LinkDir = #-L
下面我就用这个模板去编译一个opencl的小程序。
文件树如下:
.
├── convolve.cl
├── convolve_cl.cpp
├── makefile
└── makefile~
很简单的只有3个文件, convolve.cl, convolve_cl.cpp, makefile.
convolve_cl.cpp源码:
// newTutorial1.cpp : Defines the entry point for the console application. // //#include "stdafx.h" #include <CL/cl.h> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <iostream> #include <fstream> #include <string.h> #include <string> #include <pthread.h> using namespace std; #pragma comment (lib,"OpenCL.lib") #define WIDTH 1920 #define HEIGHT 1080 #define FRAMES 1 typedef unsigned char uint8_t; static double now_ms(void) { struct timespec res; clock_gettime(CLOCK_REALTIME, &res); return 1000.0 * res.tv_sec + (double) res.tv_nsec / 1e6; } static int filter0(uint8_t *src, uint8_t *dst, uint8_t *ocl, int w, int h) { printf("filter 0 ,w=%d, h=%d ",w, h); double start, end; int i =1; int j =1; if(w > WIDTH || h > HEIGHT) return 1; //start = now_ms(); for (i = 1; i < h - 1; ++i) { for (j = 1; j < w - 1; ++j) { int index = j + i * w; uint8_t lu = src[index - 1 - w]; uint8_t lm = src[index - 1]; uint8_t ld = src[index - 1 + w]; uint8_t mu = src[index - w]; uint8_t mm = src[index]; uint8_t md = src[index + w]; uint8_t ru = src[index + 1 - w]; uint8_t rm = src[index + 1]; uint8_t rd = src[index + 1 + w]; int sum = lu + lm + ld + mu + mm + md + ru + rm + rd; // printf("%d, %d, %d, %d, %d, %d, %d %d, %d, ",lu, lm, ld, mu, mm, md, ru, rm, rd); dst[index] = (uint8_t)sum / 9 + 1; // printf(" dst[%d] =%d",index, dst[index]); if(ocl[index] != dst[index]) printf("index[%d] differ ", index); } // printf(" "); } end = now_ms(); //printf("filter 0 %f ", end - start); return 0; } //°ÑÎıŸÎÄŒþ¶ÁÈëÒ»žöstringÖÐ int convertToString(const char *filename, std::string& s) { size_t size; char* str; std::fstream f(filename, (std::fstream::in | std::fstream::binary)); if(f.is_open()) { size_t fileSize; f.seekg(0, std::fstream::end); size = fileSize = (size_t)f.tellg(); f.seekg(0, std::fstream::beg); str = new char[size+1]; if(!str) { f.close(); return NULL; } f.read(str, fileSize); f.close(); str[size] = '