I am compiling the following code using clang++ and the compiler keeps erroring out with undefined reference to anything in the Link class. I assumed the compiler must not be getting the link.cpp definitions for all of the functions and constructors in the cpp file but I don't know if its some kind of include error or a makefile error at this point. Also this makefile outputs as a.out but I tohught the -o flag should make it output as prog1.
我正在使用clang ++编译以下代码,并且编译器通过对String类中的任何内容的未定义引用来保持错误。我假设编译器不能获取cpp文件中所有函数和构造函数的link.cpp定义,但我不知道它是否存在某种包含错误或makefile错误。另外这个makefile输出为a.out但是我要将-o标志输出为prog1。
main.cpp
#include <iostream>
#include <link.h>
int main (int argc, const char * argv[]) {
int i = 10;
while(i >= 0) {
i--;
}
Link test = new Link();
std::cout << "Hello, World!2\n";
return 42;
}
link.h
#ifndef LINK_H
#define LINK_H
#include <string>
using namespace std;
class Link {
private:
string * value;
Link * next;
public:
Link(Link * nextLink, string * stringValue);
~Link();
}
#endif
link.cpp
#include <link.h>
Link::Link(Link * nextLink, string * stringValue) {
this.next = nextLink;
this.value = stringValue;
}
Link::~Link() {
delete value;
}
Link * Link::getNext() {
return next;
}
string * Link::getString() {
return value;
}
void Link::printAll() {
if (next != NULL) {
cout << value << "\n" << next->printAll();
} else {
cout << value << "\n";
}
}
makefile
main.o: main.cpp
clang++ -g -Wall -Wextra main.cpp
link.o: link.cpp link.h
clang++ -g -Wall -Wextra link.cpp link.h
all: main.o link.o
clang++ -g -Wall -Wextra main.o link.o -o prog1
2 个解决方案
#1
0
Use the -c
flag to compile only without linking. The default make target is the one that comes first; you probably want this to be "all". Also, you should not pass a header file on the command line.
使用-c标志仅在不进行链接的情况下进行编译。默认的make目标是第一个;你可能希望这是“全部”。此外,您不应该在命令行上传递头文件。
all: main.o link.o
clang++ -g -Wall -Wextra main.o link.o -o prog1
main.o: main.cpp link.h
clang++ -g -Wall -Wextra -c main.cpp
link.o: link.cpp link.h
clang++ -g -Wall -Wextra -c link.cpp
#2
0
I think it's because the include statement, change #include <link.h>
to #include "link.h"
.
我认为这是因为include语句,将#include
Also, you have some other errors, for example, you forget the semicolon after the definition of class Link.
此外,您还有其他一些错误,例如,在类Link定义后忘记了分号。
Reference: What is the difference between #include < filename> and #include “filename”?
参考:#include
#1
0
Use the -c
flag to compile only without linking. The default make target is the one that comes first; you probably want this to be "all". Also, you should not pass a header file on the command line.
使用-c标志仅在不进行链接的情况下进行编译。默认的make目标是第一个;你可能希望这是“全部”。此外,您不应该在命令行上传递头文件。
all: main.o link.o
clang++ -g -Wall -Wextra main.o link.o -o prog1
main.o: main.cpp link.h
clang++ -g -Wall -Wextra -c main.cpp
link.o: link.cpp link.h
clang++ -g -Wall -Wextra -c link.cpp
#2
0
I think it's because the include statement, change #include <link.h>
to #include "link.h"
.
我认为这是因为include语句,将#include
Also, you have some other errors, for example, you forget the semicolon after the definition of class Link.
此外,您还有其他一些错误,例如,在类Link定义后忘记了分号。
Reference: What is the difference between #include < filename> and #include “filename”?
参考:#include