简体   繁体   English

Makefile C ++ Linux的麻烦

[英]trouble with makefile C++ Linux

I am trying to set up a template folder for future projects. 我正在尝试为将来的项目设置模板文件夹。 I am going to incorporate google tests for my unit testing. 我将把Google测试纳入我的单元测试中。 The issue I'm having is my makefile is not doing what I expect. 我遇到的问题是我的makefile没有达到我的期望。 any help is greatly appreciated! 任何帮助是极大的赞赏!

Here is my project structure: 这是我的项目结构:

project_root Makefile /test <-- contains google test files /src <-- my cpp files

Here is my Makefile: 这是我的Makefile:

# Google Test location
GTEST_DIR = test

# Where to find user code.
USER_DIR = src

# Flags passed to the preprocessor.
CPPFLAGS += -isystem $(GTEST_DIR)/include

# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra -pthread

# All tests produced by this Makefile.  Remember to add new tests you
# created to the list.
TESTS = sample1_unittest

.PHONY : all
all: $(EXE)

clean :
    rm -f $(TESTS) gtest.a gtest_main.a *.o

# All Google Test headers.  Usually you shouldn't change this
# definition.
GTEST_HEADERS = $(GTEST_DIR)/include/gtest/*.h \
                $(GTEST_DIR)/include/gtest/internal/*.h

# Builds gtest.a and gtest_main.a.
GTEST_SRCS_ = $(GTEST_DIR)/src/*.cc $(GTEST_DIR)/src/*.h $(GTEST_HEADERS)

gtest-all.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest_main.cc

gtest.a : gtest-all.o
    $(AR) $(ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
    $(AR) $(ARFLAGS) $@ $^

# Builds a sample test.  A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main()
# function.

### these next few lines would do the job, but i'm trying to automate
### things a bit so they are commented out

#sample1.o : $(USER_DIR)/sample1.cc $(USER_DIR)/sample1.h $(GTEST_HEADERS)
#   $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1.cc
#
#sample1_unittest.o : $(USER_DIR)/sample1_unittest.cc \
#                     $(USER_DIR)/sample1.h $(GTEST_HEADERS)
#   $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/sample1_unittest.cc
#
#sample1_unittest : sample1.o sample1_unittest.o gtest_main.a
#   $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@


# here is where my trouble begins:
SRC = $(USER_DIR)/sample1.cc $(USER_DIR)/sample1_unittest.cc
OBJ=$(SRC:.cc=.o)
EXE=go

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


$(EXE): $(OBJ) gtest_main.a
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $< -o $@

could you review the section under the comment " # here is where my trouble begins " 您能否评论下面的部分“ # here is where my trouble begins

When I run $ make i get the following error: 当我运行$ make ,出现以下错误:

make: Nothing to be done for 'all'. make:什么都不做。

I've been using a similar makefile for quite a while, but now that I've addapted it for unittesting and moved the src files into their own folder it has broken. 我使用类似的makefile已经有一段时间了,但是现在我已经对其进行了单元测试,并将src文件移到了自己的文件夹中,该文件已经损坏了。

I will be using this for all future builds so I'd really like to get this setup well now. 我将在以后的所有构建中使用此工具,因此我现在真的很想好好进行此设置。

Thank you in advance for any assistance you can provide. 预先感谢您提供的任何帮助。

Thank you for the fix. 谢谢您的修复。

I moved the the following 3 lines above .phony : all 我将以下三行移到.phony上方:全部

SRC = $(USER_DIR)/sample1.cc $(USER_DIR)/sample1_unittest.cc
OBJ=$(SRC:.cc=.o)
EXE=go

I also had an error at the end, the vary last line should have been: 最后我也有一个错误,最后一行应该是:

$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

It looks like EXE is undefined when called in all: $(EXE) . all: $(EXE)调用EXE时,似乎EXE是未定义的all: $(EXE) It only gets defined much lower. 它的定义要低得多。 So your all: line is only all: when translated. 因此,您的all:行仅是all: :。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM