标签: linker-errors

为什么我必须在这里明确声明内联函数?

好吧,到目前为止,我认为头文件中定义的函数被视为内联函数,就像模板的东西一样,定义一次,等等。

我也使用包含防护,但我仍然遇到多个已定义对象的链接器错误,我知道这是因为所有这些不同的单元都复制了链接器试图挑选出哪个项目是正确的内容。

我还知道内联只是一个建议,甚至可能不会被编译器使用等。

然而,我必须在我编写的只有标头的小工具集中显式定义所有这些小函数。

即使函数很大,我也必须将它们声明为内联,并且编译器仍然可能忽略该提示。

但无论如何我必须这样定义它们。

例子:

#ifndef texture_math_h__
#define texture_math_h__

float TexcoordToPixel(float coord, float dimension)
{
    return coord * dimension;
}

float PixelToTexcoord(float pixel, float dimension)
{
    return pixel / dimension;
}

float RecalcTexcoord(float coord,float oldDimension, float newDimension)
{
    return PixelToTexcoord(TexcoordToPixel(coord,oldDimension),newDimension);
}
#endif // texture_math_h__
Run Code Online (Sandbox Code Playgroud)

对于包含该文件的每个单元,错误是 blabla 已在 xxx.obj 中定义

当我声明所有这些内联时,它链接正确。

这是什么原因呢?这不是一个大问题,而且,优化也可能在 cpp 中找到,对吗?

我只是好奇为什么会出现在这里,希望它不会太多重复,谢谢您的宝贵时间。

c++ inline linker-errors

0
推荐指数
1
解决办法
388
查看次数

为什么非纯虚方法必须在基类中定义?

在这种特殊情况下,为什么我必须在基类中定义非纯虚方法以避免链接器错误?

这会产生链接器错误:

class A
{
  public:
  virtual ~A(){}
  virtual void foo() = 0;
  virtual void bar();
};

class B : public A
{
  public:
  void foo()
  {
  }
  void bar()
  {
  }
};

int main()
{
    B b;
}
Run Code Online (Sandbox Code Playgroud)

输出:

/tmp/cc5E8Tit.o: In function `A::~A()':
:(.text._ZN1AD2Ev[_ZN1AD5Ev]+0x13): undefined reference to `vtable for
A' /tmp/cc5E8Tit.o:(.rodata._ZTI1B[_ZTI1B]+0x10): undefined reference
to `typeinfo for A' collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

但如果我在类中定义 bar 方法,A它链接正常:

class A
{
  public:
  virtual ~A(){}
  virtual void foo() = …
Run Code Online (Sandbox Code Playgroud)

c++ virtual-functions linker-errors pure-virtual

0
推荐指数
1
解决办法
708
查看次数

为什么在编译项目时出现“多重定义”错误?

编辑:在 C 模块中初始化我的项目的常量并在相应的标头中声明它们解决了问题。

我正在致力于制作 AES 密码的 C 实现(链接: https: //github.com/SuperTotoGo/AES_Cipher)。一切都工作正常,直到我尝试将我的项目分成模块。现在,当我编译时,我收到此错误:

gcc -std=c99 -g -o aes.out *.c 
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:8: multiple definition of `AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:8: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:28: multiple definition of `INV_AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:28: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:48: multiple definition of `AES_LOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:48: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:68: multiple definition of `AES_ALOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:68: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:88: multiple definition of `AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:88: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:96: multiple definition of `INV_AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:96: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:104: multiple …
Run Code Online (Sandbox Code Playgroud)

c linker-errors

0
推荐指数
1
解决办法
4407
查看次数

链接时为什么会出现未定义的符号?

我有两个类Triangle,ALine我想在其构造函数中ALine为属性分配新实例Triangle.但是我收到了这个错误

Undefined symbols for architecture x86_64:
  "ALine::ALine()", referenced from:
      Triangle::Triangle(triangle) in Triangle.o
ld: symbol(s) not found for architecture x86_64
Run Code Online (Sandbox Code Playgroud)

以下是我写的代码:

ALine.cpp

#include "Geometry.h"
#include "ALine.h"



ALine::ALine(point a, point b)
{       
        double tmpy = a.y - b.y;
        double tmpx = a.x - b.x;
        double tmpk;
        if(equals(tmpy, 0))
        {
            tmpk = 0;
        }
        else
        {
            tmpk = tmpx/tmpy;
        }
        double tmpq = a.y - tmpk*a.x;
        if(equals(tmpx, 0))
        {
            if(equals(a.x, 0))
            {
                if(equals(b.x, 0)) tmpk …
Run Code Online (Sandbox Code Playgroud)

c++ linker-errors ld

-1
推荐指数
1
解决办法
177
查看次数

C++链接器错误Singleton类中的未定义引用

我正在尝试将此java单例类移植到c ++:

public class Singleton {
private static Singleton uniqueInstance;

private Singleton() {} 
public static Singleton getInstance() {
    if (uniqueInstance == null) {
        uniqueInstance = new Singleton();
    }
        return uniqueInstance;
    }
}
Run Code Online (Sandbox Code Playgroud)

我移植到这个C++代码:

class Singleton {
private:
  Singleton() {
    cout << "new Singleton is called" << endl;
  }
  static Singleton* uniqueInstance;
public:
   static Singleton* getInstance() {
    if (!uniqueInstance) {
      uniqueInstance = new Singleton();
    }
    return uniqueInstance;
  }
};
Run Code Online (Sandbox Code Playgroud)

但是我无法编译这个!和gcc链接器发生错误.

c++ singleton design-patterns linker-errors

-1
推荐指数
1
解决办法
3130
查看次数

编译器没有看到我的主要功能(Undefined reference to main)

我的项目中有2个cpp和3个头文件.当我在VS中编译它时,它运行顺利,我没有收到错误消息.但是当我尝试通过这条线在SSH网络上编译时:

g++ -o program.cpp lineoperations.cpp customer.h transaction.h lineoperations.h
Run Code Online (Sandbox Code Playgroud)

它说:

In function `_start':
(.text+0x20): undefined reference to `main'
Run Code Online (Sandbox Code Playgroud)

不要说"别忘了写主函数",因为它已经存在并且我的项目在VS上工作.该怎么办?

这是我的代码中的相关部分.Program.cpp直到main:

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include "lineoperations.h"
using namespace std;

line bankline;
bool operate(char);
void search(char[]);
void add(char[]);
void removee(char[]);
void transaction();
void printline();

int main(){
    bankline.create();
    bool end = false;
    while (!end) {
        end = bankline.decideFunction();
    }
    bankline.close();
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

它继续,但我猜没有必要粘贴它们.如果你需要查看其他cpp文件或头文件,我也会粘贴它们.

c++ compiler-errors linker-errors undefined-reference

-1
推荐指数
1
解决办法
616
查看次数

目标文件中的重复符号:

我正在尝试编译两个.cpp文件,(foo.cpp和bar.cpp)并构建一个共享对象(project.so).但是编译失败并且(我的一部分)错误是:

....
duplicate symbol _n in:
foo.o
bar.o
ld: 5 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1(use -v to see 
invocation)
make: *** [project.so] Error 1
ERROR: compilation failed for package ‘project’
Run Code Online (Sandbox Code Playgroud)

我的.cpp文件有几个常见的和不常见的头文件,一些常见和不常见的命名函数,以及一组通常命名的变量:

Foo.cpp中

#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include <cmath>

size_t m1;
double k1=2.0;
std::vector<double> x,y;
std::vector<double> z;
size_t n,p;

void inputfoo(){...}
void output(){...}
Run Code Online (Sandbox Code Playgroud)

bar.cpp

#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
#include <cmath>
#include "Eigen/Dense"
#include "Eigen/Cholesky" …
Run Code Online (Sandbox Code Playgroud)

c++ r compilation linker-errors

-1
推荐指数
1
解决办法
788
查看次数

c ++ oop编程

project1.cpp

#include "stdafx.h"
#include "Bicycle.cpp"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    bool runP = true;
    do {
    Bicycle object();
    char oType;
    cout << "Would you like a (B)icycle, or (A)nimal? E for Exit\n";
    cin >> oType;

    if (oType == 'B' || oType == 'b') {
        int seat, wheels;
        string brand;
        cout << "How many wheels does the bike have?\n";
        cin >> wheels;
        object().setWheels(wheels);
        cout << "How many seats does the bike have?\n";
        cin >> seat;
        object().setSeats(seat);
        cout …
Run Code Online (Sandbox Code Playgroud)

c++ linker-errors

-2
推荐指数
1
解决办法
209
查看次数

(.text+0x20): 对“main”的未定义引用是什么意思

有人可以帮助我解决下面代码清单中出现的错误吗?

该错误很可能与构造函数/析构函数有关:

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: 在函数 `_start' 中:

(.text+0x20):对“main”的未定义引用

collect2:错误:ld 返回 1 退出状态

代码清单:

                         // counter_id.cpp
                        // Obco class member-function  definitions.
   #include <iostream>

   #include "counter_id.h"  // include counter class definition

   using namespace std;

    // constructor sets object's ID number and descriptive message
   Obco::Obco( int ID, string messageString )
  : objectID( ID ), message( messageString )
     {
   cout << "Object " << objectID << "constructor runs"
     << message << endl;
     } // end CreateAndDestroy constructor


     // destructor
    Obco::~Obco()
   {
   // output newline …
Run Code Online (Sandbox Code Playgroud)

c++ linker-errors

-3
推荐指数
1
解决办法
6170
查看次数

警告:mysql_real_escape_string()[function.mysql-real-escape-string]:无法建立到服务器的链接

我在Internet webserver中执行我的PHP脚本时收到此警告消息,但在本地wamp中它正在运行而没有警告.是什么原因 ?

Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in /home/deport/public_html/abc/index.php on line 209
Run Code Online (Sandbox Code Playgroud)

php mysql linker-errors

-6
推荐指数
1
解决办法
1万
查看次数