Sep 29, 2009

Linux 下开发环境笔记

经过多种IDE的尝试,发觉还是 Eclipse 最适合我。Emacs 太烦,学习曲线太陡峭。

将别人的 project 导入Emacs:
选择新建 Makefile project->Empty project, 目标目录选择该 project,即可。非常方便。

GCC 问题:
关于 -g -On 的关系
1. 不设 -O 相当于 -O0,就是不优化,这适合于调试,也就是说 -g 要单独使用,不加优化
2. -O 相当于 -O1
3. -O2 一般来说最佳,没有 speed-space  trade-off
4. -O3 做了 speed-space trade-off,理想目标是使程序变大,换取速度,但是有些时候反而会弄巧成拙。

所以一般来说,
  • -g -O0
  • -O2
是最好的选择

传递参数给 linker ld
-Wl,option
Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas. You can use this syntax to pass an argument to the option. For example, `-Wl,-Map,output.map' passes `-Map output.map' to the linker. When using the GNU linker, you can also get the same effect with `-Wl,-Map=output.map'. 

Creating a shared and static library with the gnu compiler [gcc]

Makefile:
注意 link 的时候,库文件要放在 -o 前面,或放在最后,不能放在 -L 后面
ps: 第一版 makfile 没有遵循这个规则,我深受其害啊
# You can use '\' to extend command to several lines


###             Flags                #########

#uncomment this line to compile in debug mode
DEBUG = true

CC = g++

ifdef DEBUG
 CXXFLAGS = -g -Wall -fmessage-length=0
else
 CXXFLAGS = -O2 -Wall -fmessage-length=0
endif

LDFLAGS = -Wl,--enable-auto-import

###      User defined Variables       ########



# Other pre-existing objs needed, but not to rebuilt    

NOT_BUILD_OBJS = 
#$(LIBPMK_OBJS)



###   Include and library directory  #########

INC = -Id:\Library\OpenCV-2.1.0-win32-dll\include\opencv


LIBDIR = -Ld:\Library\OpenCV-2.1.0-win32-dll\lib


ifdef DEBUG
 LIB = -lcxcore210d.dll -lcv210d.dll -lhighgui210d.dll -lcvaux210d.dll -lml210d.dll -lopencv_ffmpeg210d.dll
else
 LIB = -lcxcore210.dll -lcv210.dll -lhighgui210.dll -lcvaux210.dll -lml210.dll -lopencv_ffmpeg210.dll
endif


###  Put here which OBJS to be built    

OBJS = main.o


###  Target    

TARGET = main.exe


###  Maybe you DON'T have to change this part    

all: $(OBJS)
 $(CC) $(LDFLAGS) $(LIBDIR) $(NOT_BUILD_OBJS) $(OBJS) $(LIB) -o $(TARGET) 

%.o: %.cxx
 $(CC) $(CXXFLAGS) $(INC) -c $< -o $@

%.o: %.cpp
 $(CC) $(CXXFLAGS) $(INC) -c $< -o $@
 
%.o: %.cc
 $(CC) $(CXXFLAGS) $(INC) -c $< -o $@
 
%.o: %.c
 $(CC) $(CXXFLAGS) $(INC) -c $< -o $@

clean:
 rm -f $(OBJS) $(TARGET)
注意,如果 make 出现如下错误,则可能是 indent 的问题,统一indent 为 tab

Makefile:73: *** missing separator. Stop.

0 comments: