makefile - c++ 类声明和定义分离后出现的问题
PHP中文网
PHP中文网 2017-04-17 11:40:59
0
5
932

我用c++写了一个小型的project,main.cpp中,需要包括三个头文件,lexer.h,parser.h,interpreter.h
这三个头文件分别有一个同名的类的声明和实现,编译和运行都是成功的,但现在我想把类的声明和实现分开,比如lexer类的声明在lexer.h,实现在lexer.cpp,打算在makefile中先分别编译再链接,但分离后会出现一些函数和变量多次声明的error,我想知道应该怎么解决?

补充以下,代码长度略长,只贴出部分:
makefile:

srcpath = src
.PHONY: all clean
all: main
main: $(srcpath)/main.o $(srcpath)/lexer.o
    g++ -o main $(srcpath)/main.o $(srcpath)/lexer.o
main.o: $(srcpath)/main.cpp
    g++ -c $(srcpath)/main.cpp
lexer.o: $(srcpath)/lexer.cpp
    g++ -c $(srcpath)/lexer.cpp
clean:
    rm -rf main

在这里我只把lexer.h拆成了lexer.h(类声明)和lexer.cpp(类实现)。
报错信息如下:
src/lexer.o:(.bss+0x0): MAX_NUM'被多次定义 src/main.o:(.bss+0x0):第一次在此定义 src/lexer.o:在函数‘getString(int)’中: lexer.cpp:(.text+0x0):getString(int)'被多次定义
src/main.o:main.cpp:(.text+0x0):第一次在此定义
src/lexer.o:在函数‘compNums(Token, Token, int)’中:
……
其中报错为多次定义的这些变量/函数,我定义在token.h中,这个头文件在lexer.h和parser.h都有包含,每个头文件我都有声明防止重复包括的宏,代码如同:

#ifndef LEXER_H
#define LEXER_H
//my code
#endif

谢谢!

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(5)
巴扎黑

The following description is for personal understanding only...Please pat me lightly

Local variable function:

If it is only used in classA.cpp, then define it directly in the classA.cpp head;

Global variable function:

You can create two additional files: global.cpp/global.h
Define in global.cpp, then export global.h in extern,
Then you can use the above variables or functions in the classA.cpp, classB.cpp header #include "global.h".

Directly defining variable functions in XXX.h files that need to be cross-included will cause problems

I was frustrated when I used VC6.0 before. It was a little better in VS2012. There is a line at the top of the new class header file by default: #pragma once

左手右手慢动作

You'd better post the code. I don't know if the one you define, such as MAX_NUM, has a const type. If it has a const type, it seems that multiple inclusions will cause a redefinition error, even if you add the beginning to prevent duplication. It is useless to include it (at least in my code). If it is a macro definition, it will generally not be reported for redefinition. . . .

左手右手慢动作

Are

MAX_NUM and getString(int) defined in token.h? Post their codes.
Post the inclusion relationship between each source file and the header file.

It feels like the variables and functions you defined in the header file are included in multiple source files, so there is a problem with the link.

迷茫

The problem is solved. I'm sorry for not posting the code to everyone. My solution is to split the declaration and implementation, and change the original .h to include .cpp. That'll be fine

黄舟

I suspect there is something wrong with the way your .cpp files are written. They all contain .h files but not .cpp files. Otherwise, why would you need .h files? And according to the normal writing method, compilation containing the .cpp file will report an error, because the definition of the class is in the .h file, and the .cpp file does not contain the definition of the class. It is recommended that you hang the .cpp file code. The problem is not as simple as including the .cpp file.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template