是否必须为虚函数定义?
请考虑以下示例程序:
#include <iostream>
using namespace std;
class base
{
public:
void virtual virtualfunc();
};
class derived : public base
{
public:
void virtualfunc()
{
cout << "vf in derived class\n";
}
};
int main()
{
derived d;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这给出了链接错误:
在function
base::base():: undefined引用中vtable for base
我没有基类中虚函数的定义.为什么即使我没有显式调用虚函数,也会发生此错误?
我发现有趣的是,如果我没有实例化类的对象derived,则链接错误不再存在.为什么是这样?什么实例化与上述链接错误有关?
我来这里是为了解决令我感到沮丧的情况.很多.首先,我在Windows上,我使用MinGW作为编译器(C++).
我在使用cpp-netlib和SSL(尝试POST到https站点)的程序中遇到了一些问题.我相信一切都井井有条,除了这一个不断躲避我的错误.
C:\boost_1_50_0\boost_1_50_0\stage\lib\libboost_thread-mgw46-mt-1_50.a(thread.o):thread.cpp|| undefined reference to 'boost::chrono::system_clock::now()'
我确信我已经链接到chrono,以及所有的.a libs BOOST_ROOT/stage/lib.我已经尝试重新排序,所以chrono在线程之前被链接.什么都没有帮助.我已经尝试BOOST_CHRONO_INLINED在我的IDE设置和多个conf.hpps中定义它仅使其成为标题,这没有帮助.
我很确定这是一个新手问题,我希望有人能给我一个快速解决方案.我匆匆写了这篇文章,因为我必须在某个地方,但如果你需要更多信息请说出来,我回家后可以更仔细地写.谢谢!
当我在我的main函数中调用模板类"add"和"greater"中的两个函数时,我一直得到未定义的引用.
所以,我有:number.h
#ifndef NUMBER_H
#define NUMBER_H
template <class T>
class number {
public:
T x;
T y;
number (int a, int b){
x=a; y=b;}
int add (T&);
T greater ();
};
#endif
Run Code Online (Sandbox Code Playgroud)
number.cpp
#include "number.h"
template <class T>
int number<T>::add (T& rezAdd){
rezAdd = x+y;
return 1;
}
template <class T>
T number<T>::greater (){
return x>y? x : y;
}
Run Code Online (Sandbox Code Playgroud)
我的主文件是:resolver.cpp
#include <stdio.h>
#include <stdlib.h>
#include "number.h"
int main (int argc, char **argv) {
int aux;
number<int> c(3,5);
c.add(aux);
printf …Run Code Online (Sandbox Code Playgroud) 我正在使用arm-linux-gnueabi-g ++ 4.7.3版进行编译.
我在位置安装了arm-linux-gnueabi库:
/ usr/arm-linux-gnueabi/lib,它包含libdl.a,libdl.so,libdl.so.2和libdl-2.19.so.
libdl.so链接到libdl.so.2,链接到libdl-2.19.so.
我试图链接到dl库(请参阅下面的命令字符串),但我总是得到未定义的引用错误.
arm-linux-gnueabi-g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -L/usr/arm-linux-gnueabi/lib -lComms -lConsole -lUtilities -ldl
../../work/libUtilities.so: undefined reference to `dlsym'
../../work/libUtilities.so: undefined reference to `dlopen'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
如果我使用g ++ 4.8.2使用以下命令编译,那么我的程序编译,链接和执行正常.
g++ -I. -I../ -I../Comms/Linux -Wall -DLINUX -fpic -o ../../work/MyProgram main.o
-L../../work -lComms -lConsole -lUtilities -ldl
Run Code Online (Sandbox Code Playgroud)
显然它找不到libdl.so库; 我认为通过使用-L标志将路径添加到相应库的位置将解决问题,但事实并非如此.
我对ARM编译器命令缺少什么?
用来libcurl编写一些测试代码.当尝试编译时,它表示未定义的引用.已经使用-lcurl或-L编译选项.
root@ubuntu:~/work/test/curlTest# curl-config --libs
-L/usr/lib/x86_64-linux-gnu -lcurl
root@ubuntu:~/work/test/curlTest# gcc -L/usr/lib/x86_64-linux-gnu -lcurl curl.c -o curl
/tmp/ccnFnpaW.o: In function `main':
curl.c:(.text+0xb1): undefined reference to `curl_global_init'
curl.c:(.text+0xbc): undefined reference to `curl_easy_init'
curl.c:(.text+0x109): undefined reference to `curl_easy_setopt'
curl.c:(.text+0x136): undefined reference to `curl_easy_setopt'
curl.c:(.text+0x145): undefined reference to `curl_easy_perform'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud) 我已经将几个资源文件转换为.obj文件objcopy,并将它们与我的程序源代码链接起来.我可以使用以下代码很好地访问程序中目标文件内的符号,但只能使用GCC/G ++(Cygwin):
extern uint8_t data[] asm("_binary_Resources_0_png_start");
extern uint8_t size[] asm("_binary_Resources_0_png_size");
extern uint8_t end[] asm("_binary_Resources_0_png_end");
Run Code Online (Sandbox Code Playgroud)
该代码在Visual Studio中不起作用,可能是因为VS拥有自己的__asm命令.我希望.data通过链接它们将我的程序资源(图像,着色器等)包含在我的最终可执行文件部分中.
但是如何在VC++中访问目标文件中定义的符号?我尝试了extern uint8_t _binary_Resources_0_png_start[]或extern "C" uint8_t _binary_Resources_0_png_start[]没有汇编命令,但我得到了未解决的符号链接错误.
我在Qt中有一个插槽的构建错误.我有一个有公共插槽的课程:
void doSomething();
Run Code Online (Sandbox Code Playgroud)
在这个类的构造函数中我做:
this->connect( ui->textFrom, SIGNAL(returnPressed()),
this, SLOT(doSomething()) );
Run Code Online (Sandbox Code Playgroud)
我有QLineEdit - textFrom对象.构建错误是
Run Code Online (Sandbox Code Playgroud)../moc_mainwindow.cpp:66: undefined reference to `MainWindow::doSomething()':-1:错误:collect2:ld返回1退出状态
请帮帮我 (:
我有一个加载 . dll/.so插件,它在 Linux 中工作得很好,但在 Windows(Visual Studio 2012)上,它因未定义的引用错误而失败。
该插件使用session->SendLine("bla")在 Main 可执行文件中定义的函数。(在插件中包含的 .h 中定义的会话 ans 方法类,但在 main exec 中编译的 .cpp 中的实际功能)。
tl; dr:“我需要 Windows 链接器忽略插件中未定义的引用,在主可执行文件中定义”
在 Windows 中“使其工作”但在没有百万#ifdef的情况下保持与 Linux 兼容的最佳方法是什么?
我试图让SFML 2.1与MingW一起工作,但这会导致问题.
我在MingW编译器中的编译行是:
g++ -ID:\SFML-2.1\include -LD:\SFML-2.1\lib main.cpp -lsfml-graphics -lsfml-window -lsfml-system
Run Code Online (Sandbox Code Playgroud)
我正在尝试链接.a文件(这是否意味着我应该添加soemthing to eh compile line?).
代码如下:
#include <SFML/Graphics.hpp>
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试以下代码:
#ifndef _GENERATEMPLATE_H_
#define _GENERATEMPLATE_H_
#include <iostream>
template <class T>
class GeneralTemplate
{
public:
GeneralTemplate();
GeneralTemplate(const GeneralTemplate &g);
~GeneralTemplate();
GeneralTemplate& operator= (GeneralTemplate const& g);
template <class M>
void arbitraryFunction(const M &m);
};
#endif
Run Code Online (Sandbox Code Playgroud)
#include "GeneralTemplate.h"
#include <iostream>
int main()
{
GeneralTemplate<int> gInt;
gInt.arbitraryFunction(2.3);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
请注意,我没有任何类模板的成员函数的实现.但那不是问题.我知道怎么做!如果我尝试编译main.cpp,我应该得到一个链接错误,这就是我得到的.问题是为什么它试图找到两次析构函数(下面的两行错误).
$g++ main.cpp
/tmp/cckrdPCs.o: In function `main':
main.cpp:(.text+0x13): undefined reference to `GeneralTemplate<int>::GeneralTemplate()'
main.cpp:(.text+0x34): undefined reference to `void GeneralTemplate<int>::arbitraryFunction<double>(double const&)'
main.cpp:(.text+0x45): undefined reference to `GeneralTemplate<int>::~GeneralTemplate()'
main.cpp:(.text+0x61): undefined reference to `GeneralTemplate<int>::~GeneralTemplate()'
collect2: ld returned …Run Code Online (Sandbox Code Playgroud)