[英]Static lib in c++ not working
我有文件夾ClientServer
。 在這個文件夾中,我還有另外兩個文件夾, Database
和Server
。 在Server
文件夾中,我有使用“ database.h”和“ newsgroup.h”的類。 這些文件位於Database
文件夾中。 我用這個make文件創建了一個lib。 我將libfile移到ClientServer
文件夾。 然后,我嘗試在Server
文件夾中調用Make; 我出錯了。
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 $@; \
$(CPP) -MM $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
# Include the *.d files
SRC = $(wildcard *.cc)
include $(SRC:.cc=.d)
實際上,一個庫只包含“功能主體”。
您仍然必須聲明函數的原型(包括.h
文件),並使編譯器可以訪問它。
因此,在您的情況下,使newsgroup.h
可被編譯器訪問(在這種情況下,將它與ans.h
文件放在同一文件夾中),它應該可以解決問題。
newsGroup.h
放在目錄結構中的什么位置? .cpp
文件是否在同一目錄中看到它? 否則,請使用-I
選項,以告知編譯器可以在哪個目錄中找到此文件。
將-I <path_to>/Database
添加到您的CXXFLAGS
,其中<path_to>
可能是用於運行編譯器的工作目錄的完整路徑或相對路徑:
CXXFLAGS += -I<path_to>/Database
另一個選擇是在#include
語句中指定相對路徑,例如在ans.h
寫入
#include "Database/newsGroup.h"
並具有-I
選項僅指向<path_to>/
。
由於.d
文件在生成時將接收這些路徑並指定依賴關系,此時還應從make
的工作目錄中查看相對於.h
依賴關系。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.