c++中的静态库不起作用
Static lib in c++ not working
我有文件夹ClientServer
。在这个文件夹中,我有另外两个文件夹,Database
和Server
。在Server
文件夹中,我有使用"database.h"answers"新闻组.h"的类。这些文件在Database
文件夹中。我用这个make文件创建了一个库。我将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)
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
依赖项。
实际上,标准库只保存了"函数体"。
您仍然需要声明函数的原型(包括.h
文件),并使编译器可以访问它。
所以,在你的情况下,让你的编译器可以访问newsgroup.h
(在这种情况下,把它放在与ans.h
文件相同的文件夹中),它应该解决问题。
相关文章:
- C++实用程序::转换在静态链接库中不起作用
- 针对静态库的 G++ 链接不起作用
- 强制链接静态库与 cmake 不起作用
- 静态使用 make_unique 和 make_shared不起作用
- 当应用于静态方法时,为什么constexpr属性不起作用
- CMake:使静态库有效,但动态库不起作用
- 在声明节点创建链接列表时,为什么静态内存分配不起作用
- Boost Logger静态链接:不起作用
- 静态控制滚动条不起作用 Win32
- 称为 C 静态库,所有内容都包装在外部"C"中,但它仍然不起作用
- 通过JNI调用简单的java静态方法是不起作用的,尽管c++编译并运行它
- 静态函数调用不起作用 (C++)
- 如果我在静态成员中使用,Qt tr() 不起作用
- 定位在静态控制中不起作用
- 为什么其中一个模板静态函数有效,而另一个不起作用
- 为什么静态流不起作用
- OpenCV和Visual Studio 2015:为什么包含静态库不起作用
- 静态私有变量在c++中不起作用
- 在 c++ 中实现静态构造函数不起作用
- 为什么这个静态断言不起作用