这是我一直在收到的错误。C++中的未定义引用
-bash-4.1$ g++ strl.cpp -o -lc-
/tmp/ccRpiglh.o: In function `main':
strl.cpp:(.text+0xf): undefined reference to `plusplus'
collect2: ld returned 1 exit status
听说是strl.cpp的源代码。这是一个重复我的问题的简单例子。
strl.cpp
#include <iostream>
#include <stdlib.h>
#include "libc-.h"
using namespace std;
int main()
{
cout<<plusplus(5,2)<<'\n';
}
这里是源libc.cpp
libc.cpp
#include <iostream>
using namespace std;
int plusplus(int a, int b)
{
return a + b;
}
来源为的libc-.H
libc-.h
#ifndef _SCANTYPE_H_
#define _SCANTYPE_H_
#include <iostream>
#include <stdlib.h>
#ifdef __cplusplus
extern "C"
{
#endif
using namespace std;
int plusplus(int a, int b);
#ifdef __cplusplus
}
#endif
#endif
我与编译如下:
g++ -Wall -shared -fPIC -o libc-.so libc-.cpp
g++ strl.cpp -o -lc-
g++ -Wall -shared -fPIC -o libc-.so libc-.cpp
编译没有错误。
为什么函数plusplus
是未定义的引用?
感谢您对我做错什么的看法。
可能重复[什么是未定义的引用/未解析的外部符号错误,以及如何解决它?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-外部符号错误和如何-DO-修复) – Niall