2014-03-25 35 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