访问的cpp文件我已经与对象创建一个问题对居住在2名不同的目录,如下所示一个cpp文件:Makefile文件试图从其他目录
- Project-xyz
- Hello
- Hello.cpp
- World
- Main.cpp
- World.cpp
- Makefile
为了更清楚这里代码的外观(只是一个虚拟代码)。
你好/ Hello.h
#ifndef HELLO_H
#define HELLO_H
void HelloPrint();
#endif // HELLO_H
你好/ Hell.cpp
#include <iostream>
#include "Hello.h"
void HelloPrint()
{
std::cout <<"Hello" << std::endl;
}
世界/ World.h
#ifndef WORLD_H
#define WORLD_H
void WorldPrint();
#endif // WORLD_H
世界/ World.cpp
#include <iostream>
#include "World.h"
void WorldPrint()
{
std::cout <<"World!" << std::endl;
}
世界/ Makefile中 使用here给出一个makefile。哪个工作正常。但它不会创建任何obj文件。
# set non-optional compiler flags here
CXXFLAGS += -std=c++11 -Wall -Wextra -pedantic-errors
# set non-optional preprocessor flags here
# eg. project specific include directories
CPPFLAGS +=
# find cpp files in subdirectories
SOURCES := $(shell find . -name '*.cpp')
SOURCES += $(shell find ../hello -name '*.cpp')
# find headers
HEADERS := $(shell find . -name '*.h')
OUTPUT := HelloWorld
# Everything depends on the output
all: $(OUTPUT)
# The output depends on sources and headers
$(OUTPUT): $(SOURCES) $(HEADERS)
$(CXX) $(CXXFLAGS) $(CPPFLAGS) -o $(OUTPUT) $(SOURCES)
clean:
$(RM) $(OUTPUT)
我看着它采用如图所示here产生在特定目录中的OBJ文件生成文件的解决方案。
现在我试图使用下面的make文件为world/objdir/hello.o中的hello/hello.cpp文件生成obj文件。哪个不起作用。 如何让它工作?
世界/ Makefile中
GCC=g++
CPPFLAGS=-c -Wall
LDFLAGS=
OBJDIR=objdir
OBJ=$(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(wildcard *.cpp)))
TARGET=HelloWorld
.PHONY: all clean
all: $(OBJDIR) $(TARGET)
$(OBJDIR):
mkdir $(OBJDIR)
$(OBJDIR)/%.o: %.cpp
$(GCC) $(CPPFLAGS) -c $< -o [email protected]
$(TARGET): $(OBJ)
$(GCC) $(LDFLAGS) -o [email protected] $^
clean:
@rm -f $(TARGET) $(wildcard *.o)
@rm -rf $(OBJDIR)
当我尝试运行,使其抛出下面的错误。
g++ -c -Wall -c main.cpp ../hello/Hello.cpp -o objdir/main.o
g++: fatal error: cannot specify -o with -c, -S or -E with multiple files
compilation terminated.
make: *** [objdir/main.o] Error 4
它不能为hello/Hello.cpp文件生成obj文件,因此引发此错误。
从makefile大师的任何帮助表示赞赏! :)
-o必须在链接“phase”中使用:'link:$(GCC)-o $(TARGET)$(OBJ)'。顺便说一句,你的项目中没有主要 – LPs
哦,对不起,我错过了补充。这只是一个简单的代码,我试图解释这个问题。这里是 #include“../hello/Hello.h” #include“World。H” INT主要() { HelloPrint(); 器WorldPrint(); }。 – YOGI