【问题标题】:File linking with c++ makefile使用 c++ makefile 链接文件
【发布时间】:2017-08-13 18:57:51
【问题描述】:

制作文件:

INCLUDE = -I/usr/X11R6/include/
LIBDIR  = -L/usr/X11R6/lib

COMPILERFLAGS = -Wall
CC = g++
CFLAGS = $(COMPILERFLAGS) $(INCLUDE)
LIBRARIES = -lX11 -lXi -lXmu -lglut -lGL -lGLU -lm

All: project

project: main.o landscape.o point.o
    $(CC) $(CFLAGS) -o $@ $(LIBDIR) $< $(LIBRARIES)

clean:
    rm *.o

我有一个 Landscape.cpp、landscape.h、point.cpp、point.h 和 main.cpp 文件 我在 main.cpp 文件中包含“point.h”,我得到:

g++ -Wall -I/usr/X11R6/include/ -o 项目 -L/usr/X11R6/lib main.cpp -lX11 -lXi -lXmu -lglut -lGL -lGLU -lm /tmp/ccdpJ8HH.o: 在函数main': main.cpp:(.text+0x1c0): undefined reference toPoint::Point(int, int)' collect2:错误:ld 返回 1 个退出状态 Makefile:15:目标“项目”的配方失败 make: *** [项目] 错误 1

【问题讨论】:

  • 您需要将目标文件提供给构建可执行文件的行。
  • @BoBTFish 我不知道这意味着什么对不起。

标签: c++ makefile hyperlink


【解决方案1】:

project: main.o landscape.o point.o $(CC) $(CFLAGS) -o $@ $(LIBDIR) $< $(LIBRARIES)

在这里您需要链接所有 .o 文件。您在此处的规则将仅使用 main.o 文件。因为$&lt;只是第一个依赖。 $^ 应该适用于所有三个。所以试试:

project: main.o landscape.o point.o $(CC) $(CFLAGS) -o $@ $(LIBDIR) $^ $(LIBRARIES)

【讨论】:

    【解决方案2】:

    我建议你使用更完整的 Makefile。

    另外,使用CXX=g++CXXFLAGS 而不是CCCFLAGS,因为您正在编译C++ 并且make 有特殊的变量。

    这是我可以使用的 Makefile 示例。

    # Project name
    NAME=       project
    
    # Include directory
    INC_DIR=    /usr/X11R6/include/
    
    # Library directory
    LIB_DIR=    /usr/X11R6/lib/
    
    # Compiler
    CXX=        g++
    
    # Source files
    SRC_DIR=    # in case your cpp files are in a folder like src/
    
    SRC_FILES=  main.c      \
                landscape.c \
                point.c
    
    # Obj files
    OBJ=        $($(addprefix $(SRC_DIR), $(SRC_FILES)):.c=.o)
    # that rule is composed of two steps
    #  addprefix, which add the content of SRC_DIR in front of every
    #  word of SRC_FILES
    #  And a second rule which change every ".c" extension into ".o"
    
    LIBS=       X11 \
                Xi  \
                Xmu \
                glut    \
                GL  \
                GLU \
                m
    
    # Compilation flags
    CXXFLAGS=   -Wall
    
    CXXFLAGS+=  $(addprefix -I, $(INC_DIR))
    
    LDFLAGS=    $(addprefix -L, $(LIB_DIR)) \
                $(addprefix -l, $(LIBS))
    
    # Rules
    
    # this rule is only linking, no CFLAGS required
    $(NAME):    $(OBJ) # this force the Makefile to create the .o files
            $(CXX) -o $(NAME) $(OBJ) $(LDFLAGS)
    
    All:    $(NAME)
    
    # Remove all obj files
    clean:
            rm -f $(OBJ)
    
    # Remove all obj files and the binary
    fclean: clean
            rm -f $(NAME)
    
    # Remove all and recompile
    re: fclean all
    
    # Rule to compile every .c file into .o
    %.o:    %.c
            $(CXX) -o $@ -c $< $(CFLAGS)
    
    # Describe all the rules who do not directly create a file
    .PHONY: All clean fclean re
    

    我不确定它是否完美,但它更好。 另外不要忘记将您的项目规则放在您的 All 规则之前,以避免在简单地调用 make 时重新链接。

    这样,您还可以添加漂亮的消息(例如在%.o: %.c 规则中)。

    您只需执行make re 即可完全更新您的二进制文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 1970-01-01
      相关资源
      最近更新 更多