2016-07-19 75 views
0

私はMakefileには新しく、cppプロジェクトを作成しようとしています。 "hello world"プログラム(main.cppファイルのみ)があります。 私はメイクをコンパイルしようとしたときに、このエラーを取得して停止することはできません。make error: "g ++:エラー:main.o:そのようなファイルやディレクトリがありません"

g++ -std=c++0x -g -Wall -o sub_game main.o 
g++: error: main.o: No such file or directory 
g++: fatal error: no input files 
compilation terminated. 
Makefile:38: recipe for target 'sub_game' failed 
make: *** [sub_game] Error 4 

私は、私が間違っているのかを理解していないあなたの助けをいただければ幸いです。

これはMakefileのである:

# the compiler: gcc for C program, define as g++ for C++ 
CC = g++ 

# compiler flags: 
# -g adds debugging information to the executable file 
# -Wall turns on most, but not all, compiler warnings 
CXXLAGS = -std=c++0x -g -Wall 

# the build target executable: 
TARGET = sub_game 

# define any libraries to link into executable: 
LIBS = -lpthread -lgtest 

# define the C source files 
SRCS = ../src 

# define the C object files 
# 
# This uses Suffix Replacement within a macro: 
# $(name:string1=string2) 
#   For each word in 'name' replace 'string1' with 'string2' 
# Below we are replacing the suffix .cc of all words in the macro SRCS 
# with the .o suffix 
# 
#OBJ = $(SRCS)/main.cc 
OBJ = main.o 

# define any directories containing header files other than /usr/include 
# 
INCLUDES = -I../include 

all : $(TARGET) 

$(TARGET) : $(OBJ) 
      $(CC) $(CXXLAGS) -o $(TARGET) $(OBJ) 

main.o : $(SRCS)/main.cpp 

.PHONY : clean 
clean : 
    rm $(TARGET) $(OBJ) 

は、高度にありがとうございます。

+0

それはそうです大丈夫です。コンパイルのコマンドを書くようにしてください。 – EFenix

+0

私はこのルールを追加して作業しています: g ++ -c $(SRCS)/main.cpp これは私が追加する必要がありますか? – dorash

+0

私はそれが今動作すると思います...ところで、CXXFLAGS(CXXLAGSではなく)を使用してください – EFenix

答えて

1

のMakefileは、コマンドラインを毎回入力しなくても、プログラムをコンパイルして、避けるために使用されて再コンパイルする必要はありません何。

1つのファイルの小さなプロジェクトでは、毎回ファイルを再コンパイルしますが、大きなプロジェクトでは、毎回すべてを再コンパイルしないと時間が大幅に節約されます。いくつかの大きなライブラリソースで作業します)。

だから、あなたがする必要はありませんどのような再コンパイルを避けるために少しあなたのMakefileを変更する必要があります。

そのよう
SRCS = ../src/main.cpp\ #Put here the relative path to your .cpp 
     ../src/exemple_second_file.cpp 

OBJS = $(SRCS:.cpp=.o) # Here you get the .o of every .cpp 

TARGET = sub_game # The executable name 

CC = g++ 

CXXFLAGS = std=c++0x -g -Wall 

LIBS = -lpthread -lgtest 

all: $(TARGET) 

$(TARGET): $(OBJS) # This line will compile to .o every .cpp which need to be (which have been modified) 
      $(CC) -o $(TARGET) $(OBJS) $(LIBS) # Linking (no need to CXXFLAGS here, it's used when compiling on previous line 

ETC... # And so on... 

、あなたのメイクファイルは($(CXXFLAGS)を使用して自動的に$(OBJS)をコンパイルしますmain.oルール)は、少なくともLinux上で、私はwinodwsのために知っていない暗黙的である

真心を込め、 JM445

(私の英語のため申し訳ありませんが、私はフランス人だ)

+0

答えがよく説明されてくれてありがとう。 – dorash

1

それはmain.oルール怒鳴るコマンドを必要とします:

main.o : $(SRCS)/main.cpp 
    $(CC) $(CXXFLAGS) -c -o [email protected] $^ 
+0

ありがとう、あなたの助けをありがとうが、私は上記の答えを使用することを選択しました。 – dorash

関連する問題