红联Linux门户
Linux帮助

我的makefile文件有问题吗?

发布时间:2010-07-04 03:02:35来源:红联作者:vfdff
[i=s] 本帖最后由 vfdff 于 2010-7-4 03:50 编辑 [/i]

我使用 Makefile_st_task 文件作为Makefile文件,其内容:[code]CC=gcc
sources = log.c u_socket.c u_task.c
objects = $(sources:.c=.o)
include_dirs =
CFLAGS = $(include_dirs) -O0 -DU_OS=OS_LINUX -L.

all: task_o u_task

task_o: $(objects)
$(objects): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@ u_def.h

u_task:$(objects)
$(CC) -g $(CFLAGS) st_task.c $< -o $@ u_def.h


.PHONY : clean
clean :
-rm edit $(objects)[/code]然后在SUSE11.2系统中使用 make all -f Makefile_st_task 编译程序时提示找不到这个 log.o文件
但是使用ls 命令可以看到这个log.o文件已经生成了,是不是我的makefile文件编辑的有问题才导致这个问题呢 ?
[attach]31233[/attach]
文章评论

共有 10 条评论

  1. tihu1111 于 2010-10-21 17:26:18发表:

    [i=s] 本帖最后由 tihu1111 于 2010-10-21 17:35 编辑 [/i]

    files := $(shell echo src/*.c)
    myobj:=$(files:.c=.o)
    CXX=mipsel-uclibc-g++

    $(myobj):%.o:%.c

    @echo [Compile... $@]
    $(Q_)$(CXX) -c -o $@ $< $(CFLAGS)

    ktv:$(myobj)
    @echo [Link... $@]
    $(CXX) -o $@ $(filter %.o, $^) $(LDFLAGS)



    这是我的,可以make

  2. wxwp 于 2010-09-05 21:35:10发表:

    有通用的makefile..

  3. vfdff 于 2010-08-21 00:38:23发表:

    8# alick


    谢谢分享,不过我还是想问下,您的这个makefile是自己写的 还是工具软件生成的?

  4. alick 于 2010-07-06 12:41:09发表:

    这里有一个通用的makefile。(从网上看到的。我已经在使用)
    可以用作C/C++混合编译。单独编译当然也没问题。
    一般只需指定PROGRAM,CFLAGS等即可[code]###############################################################################
    #
    # Generic Makefile for C/C++ Program
    #
    # Author: whyglinux (whyglinux AT hotmail DOT com)
    # Date: 2006/03/04

    # Description:
    # The makefile searches in directories for the source files
    # with extensions specified in , then compiles the sources
    # and finally produces the , the executable file, by linking
    # the objectives.

    # Usage:
    # $ make compile and link the program.
    # $ make objs compile only (no linking. Rarely used).
    # $ make clean clean the objectives and dependencies.
    # $ make cleanall clean the objectives, dependencies and executable.
    # $ make rebuild rebuild the program. The same as make clean && make all.
    #==============================================================================

    ## Customizing Section: adjust the following if necessary.
    ##=============================================================================

    # The executable file name.
    # It must be specified.
    # PROGRAM := a.out # the executable name
    PROGRAM :=

    # The directories in which source files reside.
    # At least one path should be specified.
    SRCDIRS := . # current directory
    #SRCDIRS :=

    # The source file types (headers excluded).
    # At least one type should be specified.
    # The valid suffixes are among of .c, .C, .cc, .cpp, .CPP, .c++, .cp, or .cxx.
    # SRCEXTS := .c # C program
    # SRCEXTS := .cpp # C++ program
    SRCEXTS := .c .cpp # C/C++ program
    #SRCEXTS :=

    # The flags used by the cpp (man cpp for more).
    #CPPFLAGS := -Wall -Werror # show all warnings and take them as errors
    CPPFLAGS := -g

    # The compiling flags used only for C.
    # If it is a C++ program, no need to set these flags.
    # If it is a C and C++ merging program, set these flags for the C parts.
    CFLAGS :=
    CFLAGS += -g -Wall

    # The compiling flags used only for C++.
    # If it is a C program, no need to set these flags.
    # If it is a C and C++ merging program, set these flags for the C++ parts.
    CXXFLAGS :=
    CXXFLAGS += -Wall

    # The library and the link options ( C and C++ common).
    LDFLAGS :=
    LDFLAGS +=

    ## Customizing Section End!!!
    ##=============================================================================


    ## Implict Section: change the following only when necessary.
    ##=============================================================================
    # The C program compiler. Uncomment it to specify yours explicitly.
    CC = gcc

    # The C++ program compiler. Uncomment it to specify yours explicitly.
    CXX = g++

    # Uncomment the 2 lines to compile C programs as C++ ones.
    #CC = $(CXX)
    #CFLAGS = $(CXXFLAGS)

    # The command used to delete file.
    RM = rm -f

    ## Stable Section: usually no need to be changed. But you can add more.
    ##=============================================================================
    SHELL = /bin/sh
    SOURCES = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
    OBJS = $(foreach x,$(SRCEXTS), \
    $(patsubst %$(x),%.o,$(filter %$(x),$(SOURCES))))
    DEPS = $(patsubst %.o,%.d,$(OBJS))

    .PHONY : all objs clean cleanall rebuild

    all : $(PROGRAM)

    # Rules for creating the dependency files (.d).
    #---------------------------------------------------
    %.d : %.c
    @$(CC) -MM -MD $(CFLAGS) $<

    %.d : %.C
    @$(CC) -MM -MD $(CXXFLAGS) $<

    %.d : %.cc
    @$(CC) -MM -MD $(CXXFLAGS) $<

    %.d : %.cpp
    @$(CC) -MM -MD $(CXXFLAGS) $<

    %.d : %.CPP
    @$(CC) -MM -MD $(CXXFLAGS) $<

    %.d : %.c++
    @$(CC) -MM -MD $(CXXFLAGS) $<

    %.d : %.cp
    @$(CC) -MM -MD $(CXXFLAGS) $<

    %.d : %.cxx
    @$(CC) -MM -MD $(CXXFLAGS) $<

    # Rules for producing the objects.
    #---------------------------------------------------
    objs : $(OBJS)

    %.o : %.c
    $(CC) -c $(CPPFLAGS) $(CFLAGS) $<

    %.o : %.C
    $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<

    %.o : %.cc
    $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<

    %.o : %.cpp
    $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<

    %.o : %.CPP
    $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<

    %.o : %.c++
    $(CXX -c $(CPPFLAGS) $(CXXFLAGS) $<

    %.o : %.cp
    $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<

    %.o : %.cxx
    $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $<

    # Rules for producing the executable.
    #----------------------------------------------
    $(PROGRAM) : $(OBJS)
    ifeq ($(strip $(SRCEXTS)), .c) # C file
    $(CC) -o $(PROGRAM) $(OBJS) $(LDFLAGS)
    else # C++ file
    $(CXX) -o $(PROGRAM) $(OBJS) $(LDFLAGS)
    endif

    -include $(DEPS)

    rebuild: clean all

    clean :
    @$(RM) *.o *.d

    cleanall: clean
    @$(RM) $(PROGRAM) $(PROGRAM).exe

    ### End of the Makefile ## Suggestions are welcome ## All rights reserved ###
    ###############################################################################[/code]

  5. shenhao0129 于 2010-07-04 20:56:29发表:

    对于makefile我也是新手,呵呵,勉强的能看懂

  6. vfdff 于 2010-07-04 19:59:43发表:

    引用:
    格式错误。
    第10行最开头没有tab(或者空格等)!
    $为第一个字符。
    缩进导致make把那一行作为命令了,但是log.o不是命令。
    alick 发表于 2010-7-4 10:54


    现在使用[code]CC=gcc
    sources=log.c u_socket.c u_task.c
    objects=log.o u_socket.o u_task.o
    include_dirs=
    CFLAGS=$(include_dirs) -O0 -DU_OS=OS_LINUX

    all: $(objects) u_task


    log.o: log.c
    $(CC) -c $(CFLAGS) $< -o $@

    u_socket.o: u_socket.c
    $(CC) -c $(CFLAGS) $< -o $@

    u_task.o: u_task.c
    $(CC) -c $(CFLAGS) $< -o $@

    u_task: log.o u_socket.o u_task.o
    $(CC) -g $(CFLAGS) st_task.c log.c u_socket.c u_task.c -lpthread -o $@


    .PHONY : clean
    clean :
    rm -rf $(objects)[/code]能够对这个工程进行编译了,但是感觉这个makefile文件太不好看了,老大们帮忙改改呀

  7. vfdff 于 2010-07-04 19:03:56发表:

    gcc -c -O0 -DU_OS=OS_LINUX -L. u_socket.c u_def.h -o u_socket.o
    好像这个命令就不行
    提示 gcc: cannot specify -o with -c or -S with multiple files

  8. shenhao0129 于 2010-07-04 18:17:53发表:

    貌似提示没说文件找不到啊,说的是命令找不到

  9. vfdff 于 2010-07-04 17:19:50发表:

    2# alick

    按您的方法调整了,还是提示一个错误,这个又是为什么?
    [attach]31238[/attach]

  10. alick 于 2010-07-04 10:54:34发表:

    格式错误。
    第10行最开头没有tab(或者空格等)!
    $为第一个字符。
    缩进导致make把那一行作为命令了,但是log.o不是命令。