2014-03-25 30 views
0

我有文件夾ClientServer。 在這個文件夾中我有兩個其他文件夾,DatabaseServer。 在Server文件夾我有類使用「database.h」和「newsgroup.h」。 這些文件位於Database文件夾中。我用這個make文件創建了一個lib。我將libfile移至ClientServer文件夾。然後我嘗試在Server文件夾中調用Make;我得到錯誤。C++中的靜態庫不工作

Makefile:47: ans.d: No such file or directory 
Makefile:47: com.d: No such file or directory 
Makefile:47: myserver.d: No such file or directory 
In file included from myserver.cc:11:0: 
ans.h:4:23: fatal error: newsGroup.h: No such file or directory 

#include "newsGroup.h" 
       ^
compilation terminated. 


# 
# Makefile to make the file libclientserver.a, containing 
# connection.o and server.o 
# 
# Define the compiler. g++ can be 
# changed to clang++. 
CXX = g++ 
CC = g++ 

# Define preprocessor, compiler, and linker flags. Uncomment the # lines 
# if you use clang++ and wish to use libc++ instead of libstdc++. 
CXXFLAGS = -g -O2 -Wall -W -pedantic-errors 
CXXFLAGS += -Wmissing-braces -Wparentheses -Wold-style-cast 
CXXFLAGS += -std=c++11 
#CPPFLAGS = -stdlib=libc++ 
#CXXFLAGS += -stdlib=libc++ 
#LDFLAGS += -stdlib=libc++ 

all: libdatabase.a 

# Create the library; ranlib is for Darwin and maybe other systems. 
# Doesn't seem to do any damage on other systems. 

libdatabase.a: Database.o newsGroup.o 
    ar rv libdatabase.a Database.o newsGroup.o 
    ranlib libdatabase.a 

# Phony targets 
.PHONY: all clean 

# Standard clean 
clean: 
    rm -f *.o libclientserver.a 

# Generate dependencies in *.d files 
%.d: %.cc 
    @set -e; rm -f [email protected]; \ 
     $(CPP) -MM $(CPPFLAGS) $< > [email protected]$$$$; \ 
     sed 's,\($*\)\.o[ :]*,\1.o [email protected] : ,g' < [email protected]$$$$ > [email protected]; \ 
     rm -f [email protected]$$$$ 

# Include the *.d files 
SRC = $(wildcard *.cc) 
include $(SRC:.cc=.d) 
+0

您的依賴性處理過於複雜,並不完全正確。試試'-include $(SRC:.cc = .d)' – Beta

+0

這意味着它找不到頭文件,而不是lib。 – OMGtechy

+0

爲什麼它需要頭文件,那麼一切都應該在lib中。 – user2975699

回答

0

newsGroup.h在哪放置在您的目錄結構中? .cpp文件是否在同一個目錄中看到它?否則使用-I選項來告訴編譯器在哪個目錄中可以找到這個文件。

添加例如-I <path_to>/DatabaseCXXFLAGS,其中<path_to>可能是要麼用來運行編譯器的工作目錄的完整或相對路徑:

CXXFLAGS += -I<path_to>/Database 

另一種選擇是在#include語句指定相對路徑,例如在ans.h

#include "Database/newsGroup.h" 

,並有-I選項只是指向<path_to>/
由於.d文件將在生成時接收這些路徑,並指定該點的依賴關係還應該看到相對於make的工作目錄的.h依賴關係。

+0

newsGroup在Database文件夾中 ans類在Server文件夾中。 這兩個文件夾位於ClientServer文件夾中。 – user2975699

+0

@ user2975699因此,您需要在「CXXFLAGS」中添加諸如「-I /Database」之類的內容,其中「」可能是用於運行編譯器的工作目錄的完整路徑或相對路徑。 –

+0

CPPFLAGS = -I .. 我有另一個在Clintserver文件夾中的libfile,並且這個文件正在工作。 – user2975699

0

其實,一個圖書館只是擁有「功能的主體」。

您仍然需要聲明函數的原型(包括.h文件),並使其可供編譯器訪問。

因此,在你的情況下,你的編譯器可以訪問newsgroup.h(在這種情況下,把它放在與ans.h文件相同的文件夾中),它應該可以解決問題。

+0

非常感謝 – user2975699