如何使用 iostream、string 和 ifstream 库生成文件
我正在尝试为使用 lib 字符串 iostream 和 fstream 的应用程序构建一个 makefile。这就是我到目前为止所做的,
CPP = gcc
LIB_DIR = ./incl
PROGRAMS = test
PROGS_O = action_rec.o
CPPFLAGS = -I$(LIB_DIR) -pg -g
VPATH = ./src/
OBJFILES = $(VPATH)$(patsubst %.cpp,%.o,$(wildcard *.cpp))
LIBS = -02 -liostream -lfstream -lstdlib -lstring
当我尝试使用我的 makefile 时,我得到的结果是,所有需要 lib 字符串 fstream 和 iostream 的东西都不是声明,而所有需要 lib stdlib 的东西都可以正常工作。 有人能告诉我为什么吗? 谢谢
I'm trying to build a makefile for an application that use the lib string iostream and fstream.that's what I did up to now
CPP = gcc
LIB_DIR = ./incl
PROGRAMS = test
PROGS_O = action_rec.o
CPPFLAGS = -I$(LIB_DIR) -pg -g
VPATH = ./src/
OBJFILES = $(VPATH)$(patsubst %.cpp,%.o,$(wildcard *.cpp))
LIBS = -02 -liostream -lfstream -lstdlib -lstring
when I try to use my makefile I get as result that everything that need the lib string fstream and iostream was not declared, while everything that need the lib stdlib works properly.
can someone tell me why?
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试取出所有 -lstring -stdlib -liostream -lfstream 并查看是否修复它。
并使用 g++ 进行编译,以便它找到链接 C++ 代码所需的其他内容。
Try taking all the -lstring -stdlib -liostream -lfstream out and see if it fixes it.
And compile with g++ so it finds other things it needs to link C++ code.
这里有两个单独的问题。
C++ 方面:
不要使用
gcc
二进制文件来编译 C++ 代码 - 请改用g++
。这将自动引入所有必需的标准包含路径和库。Makefile 端:
您的 Makefile 有许多问题:
不要将
CPP
设置为编译器;该变量定义要使用的预处理器。使用CXX
定义编译器 - 它很可能恰好被定义为g++
。不要创建名为
test
的程序。这恰好与标准 Unix 程序 (/usr/bin/test
) 发生冲突,如果您不注意,您可能最终会调用错误的程序。不要通过
LIBS
将-02
作为链接器标志传递。此开关 a) 措辞错误(应该是-O2
,字母 O 不是零),b) 它是一个编译器标志,因此它应该是CXXFLAGS
的一部分.很可能您根本不需要 Makefile。 make 程序附带默认 Makefile,因此通常您只需创建一个
main.cpp
文件,然后运行这将自动编译并链接
main.cpp
为main
可执行文件。在你的情况下,类似可能就足够了。
There are two separate issues here.
The C++ side:
Don't use the
gcc
binary for compiling C++ code - useg++
instead. This will pull in all the required standard include paths and libraries automagically.The Makefile side:
Your Makefile has a number of issues:
Don't set
CPP
to a compiler; this variable defines the preprocessor to use. UseCXX
to define the compiler - it most likely happens to be defined tog++
anyway.Don't create programs with the name
test
. This happens to clash with a standard Unix program (/usr/bin/test
) and if you don't pay attention you might end up calling the wrong program.Don't pass
-02
as the linker flags viaLIBS
. This switch is a) wrongly worded (it should be-O2
, the letter O not a zero) and b) it's a compiler flag, so it should be part ofCXXFLAGS
.Chances are that you don't need a Makefile at all. The make program comes with default Makefiles so often you can just create a
main.cpp
file and then runThis will automatically compile and link
main.cpp
into amain
executable. In your case, something likeMight be sufficient.
明显的问题是您链接的是
gcc
而不是g++
。gcc
不链接到 C++ 标准库。你可以添加它手动,但最简单的就是使用
g++
来完成所有事情(除非你有实际的 C 代码;
g++
将以.c
结尾的文件编译为C++)。
The obvious problem is that you're linking with
gcc
rather thang++
.gcc
doesn't link against the C++ standard library. You can add itmanually, but the simplest is simply to use
g++
for everything (unlessyou have actual C code;
g++
will compiler files ending in.c
asC++).
LIBS = -02 -liostream -lfstream -lstdlib -lstring
可以简化很多。只需使用LIBS = -O2
(我认为您的意思是 -O2 而不是 -02。)事实上,您确实不应该将-O
标志传递给链接根本没有阶段。优化是编译时选项,而不是链接时选项。LIBS = -02 -liostream -lfstream -lstdlib -lstring
can be simplified a lot. Just useLIBS = -O2
(I think you meant -O2 rather than -02.) In fact, you really shouldn't be passing a-O
flag to the link stage at all. Optimization is a compile-time option, not a link-time option.