标签: linker-errors

如何在 Linux 中重新定义 malloc() 以在 C++ new 中使用

我为我定义了 mem_malloc() 和 mem_free(),我想用它们来替换 malloc() 和 free() 以及 C++ 的 new 和 delete。

我将它们定义如下:

extern "C" {

extern void *mem_malloc(size_t);
extern void mem_free(void *);

void *
malloc(size_t size) {
  return mem_malloc(size);
}

void
free(void *memory) {
  mem_free(memory);
}
}
Run Code Online (Sandbox Code Playgroud)

但是,我收到两个链接错误:

[user@machine test]$ g++ -m32 -pthread main.cpp -static  libmemnmf-O.a
/usr/lib/../lib/libc.a(malloc.o): In function `free':
(.text+0x153c): multiple definition of `free'
/tmp/ccD2Mgln.o:main.cpp:(.text+0x842): first defined here
/usr/lib/../lib/libc.a(malloc.o): In function `malloc':
(.text+0x3084): multiple definition of `malloc'
/tmp/ccD2Mgln.o:main.cpp:(.text+0x856): first defined here
libmemnmf-O.a(mem_debug.o): In function `mem_init_debug_routines':
mem_debug.c:(.text+0x83c): undefined …
Run Code Online (Sandbox Code Playgroud)

c++ malloc redefine linker-errors

4
推荐指数
1
解决办法
4998
查看次数

C++ 链接器抱怨 char* 的多个定义,但不抱怨 std::string

在一个大项目中,我有一个 .h 文件,它在命名空间中定义了很多常量。引入 const char* 常量会导致链接器错误,抱怨多个定义。

前h

#include <string>
namespace Dv
{
    const int MAX = 10;
    const std::string NAME = "bobo";

    const char* NAME2 = "fred";  // <-- ERROR: multiple definition of `Dv::NAME2'
}
Run Code Online (Sandbox Code Playgroud)

前A.cpp

#include "ex.h"
void aFunction() { printf("this is aFunction\n"); }
Run Code Online (Sandbox Code Playgroud)

前B.cpp

#include "ex.h"    
void aFunction(void);

int main(int argc, char **argv)
{
    aFunction();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译和链接

g++ -c exA.cpp
g++ -c exB.cpp
g++ exA.o exB.o -o ex
exB.o:(.data+0x0): multiple definition of `Dv::NAME2'
exA.o:(.data+0x0): first defined …
Run Code Online (Sandbox Code Playgroud)

c++ namespaces constants linker-errors linkage

4
推荐指数
1
解决办法
2196
查看次数

C 链接错误(使用 tcc)

我正在尝试从名为 的小型 cc (tcc-0.9.26-win64-bin.zip)运行该示例libtcc_test.c

我已将libtcc.hfrom libtccintoinclude复制libtcc.deflib.
然后我运行tcc ./examples/libtcc_test.c并收到链接错误:/

tcc: error: undefined symbol 'tcc_new'
tcc: error: undefined symbol 'tcc_set_lib_path'
tcc: error: undefined symbol 'tcc_set_output_type'
tcc: error: undefined symbol 'tcc_compile_string'
tcc: error: undefined symbol 'tcc_add_symbol'
tcc: error: undefined symbol 'tcc_relocate'
tcc: error: undefined symbol 'tcc_get_symbol'
tcc: error: undefined symbol 'tcc_delete'
Run Code Online (Sandbox Code Playgroud)

我缺少什么?


更多信息:

P:\cpp\tcc>tcc ./examples/libtcc_test.c -vv
tcc version 0.9.26 (i386 Win32)
-> ./examples/libtcc_test.c
-> p:/cpp/tcc/include/stdlib.h
->  p:/cpp/tcc/include/_mingw.h
-> …
Run Code Online (Sandbox Code Playgroud)

c linker-errors tcc undefined-symbol lib

4
推荐指数
1
解决办法
5118
查看次数

使用 Visual Studio C++ 进行单元测试时出现链接器错误

我想用 Visual Studio 对我的 C++ 项目进行单元测试。将项目中的文件夹添加为测试项目的包含路径后,尝试编译测试时出现链接器错误:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "public: __thiscall Piece::Piece(enum Color)" (??0Piece@@QAE@W4Color@@@Z) referenced in function "public: __thiscall Bishop::Bishop(enum Color)" (??0Bishop@@QAE@W4Color@@@Z)    ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1   
Error   LNK2019 unresolved external symbol "public: __thiscall Board::~Board(void)" (??1Board@@QAE@XZ) referenced in function "public: void __thiscall ChessPlusPlusTests::BishopTests::ValidMovesTest(void)" (?ValidMovesTest@BishopTests@ChessPlusPlusTests@@QAEXXZ)  ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1   
Error   LNK2019 unresolved external symbol "public: void __thiscall Board::placePieceAt(class Piece * const,struct Position)" (?placePieceAt@Board@@QAEXQAVPiece@@UPosition@@@Z) referenced in function "public: void __thiscall ChessPlusPlusTests::BishopTests::ValidMovesTest(void)" (?ValidMovesTest@BishopTests@ChessPlusPlusTests@@QAEXXZ)    ChessPlusPlus-Tests D:\Documents\Projects\ChessPlusPlus\ChessPlusPlus-Tests\BishopTests.obj 1 …
Run Code Online (Sandbox Code Playgroud)

c++ unit-testing linker-errors visual-studio

4
推荐指数
1
解决办法
4750
查看次数

_main_getcmdline 中的链接器错误 _SDL_main 未解决

我正在关注 SDL2 的 LazyFoo 教程。我经历了前十节课,现在我收到了这个错误(在我编译之前,我没有收到来自 VS 的任何错误)。

1>------ Inizio compilazione: Progetto: yetANewSDLProj, Configurazione: Debug Win32 ------
1>SDL2main.lib(SDL_windows_main.obj) : error LNK2019: riferimento al simbolo esterno _SDL_main non risolto nella funzione _main_getcmdline
1>Compilazione progetto "yetANewSDLProj.vcxproj" NON COMPLETATA.
========== Compilazione: 0 completate, 1 non riuscite, 0 aggiornate, 0 ignorate ==========
Run Code Online (Sandbox Code Playgroud)

“在函数“_SDL_getcmdline”中未解析对外部符号“_SDL_main”的引用

在我看来,错误不在我的代码中,而是在 SDL 的文件之间,但我无法弄清楚 Visual Studio 没有找到哪个文件或为什么。我创建了一个新的空项目,我唯一的代码(一个文件,main.cpp)是:

#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <cmath>

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    while(1){}
    SDL_Quit();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

包含文件位于路径:“C:\vs_dev_lib\include”
DLL/lib …

c++ linker-errors sdl-2

4
推荐指数
1
解决办法
4378
查看次数

Red Hat: using &lt;atomic&gt; compiles fine but linker can't find __atomic_store_16; what library?

I'm using atomic<> for the first time, and just as using <thread> requires you to link a thread library, it seems like using <atomic> wants you to do... something. What?

> uname -a
Linux sdclxd00239 3.10.0-693.11.6.el7.x86_64 #1 SMP Thu Dec 28 14:23:39 EST 2017 x86_64 x86_64 x86_64 GNU/Linu

> g++ Foo.cxx -g -o MsgQueueNoLock -pthread
/tmp/ccnGOKUG.o: In function `std::atomic<Ptr_T>::store(Ptr_T, std::memory_order)':
/opt/rh/devtoolset-7/root/usr/include/c++/7/atomic:239: undefined reference to `__atomic_store_16'
/tmp/ccnGOKUG.o: In function `std::atomic<Ptr_T>::load(std::memory_order) const':
/opt/rh/devtoolset-7/root/usr/include/c++/7/atomic:250: undefined reference to `__atomic_load_16'
collect2: error: ld returned 1 …
Run Code Online (Sandbox Code Playgroud)

c++ gcc redhat linker-errors stdatomic

4
推荐指数
1
解决办法
3316
查看次数

由于在 Bjarne Stroustrup "Programming and Practices using c++" 中找不到符号导致的链接错误

我是 C++(以及一般编译语言)的新手,正在 Bjarne Stroustrup“使用 C++ 进行编程和实践”的第 8 章末尾进行练习,但是当我尝试编译代码时出现以下错误

?  Desktop g++ -std=c++11 *.cpp -o use
Undefined symbols for architecture x86_64:
  "_foo", referenced from:
      print_foo() in my-4f7853.o
      _main in use-46cb26.o
     (maybe you meant: __Z9print_foov)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

我也试过使用g++ -c my.cpp use.cpp后跟,g++ -o use.exe my.o use.o但这给出了同样的错误。我尝试的另一种方法是g++ -c use.cpp -o use.exe,但是use.exe在运行时没有产生任何输出。源代码文件是

我的.h

extern int foo;
void print_foo(); …
Run Code Online (Sandbox Code Playgroud)

c++ g++ clang linker-errors

4
推荐指数
1
解决办法
121
查看次数

使用 MSVC 在 Windows 中编译 SDL2 代码时出现错误 LNK2019:无法解析的外部符号

完整的错误输出:

SDL2main.lib(SDL_windows_main.obj) : error LNK2019: unresolved external symbol __imp_CommandLineToArgvW referenced in function main_getcmdline
Run Code Online (Sandbox Code Playgroud)

我的编译器选项:

cl -D WINDOWS -nologo -W4 -WX -wd4100 -Fe"output_file.exe" input_file.c SDL2.lib SDL2main.lib -I ./SDL2-2.0.12/include -link -LIBPATH:./SDL2-2.0.12/lib/x64 -SUBSYSTEM:CONSOLE
Run Code Online (Sandbox Code Playgroud)

我的input_file.c标题和主要功能:

#ifdef LINUX
#include <SDL2/SDL.h> /* Comes with stdio.h and stdlib.h */
#elif WINDOWS
#include <stdio.h>
#include "SDL.h"
#endif

int main(int argc, char* argv[])                                                                              {
  if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER) != 0)
  {
    fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());
    return -1;
  }

. . .
Run Code Online (Sandbox Code Playgroud)

命令行参数中使用的 SDL 开发文件夹是从 libsdl.org 下载的开发库 zip …

sdl linker-errors visual-c++ sdl-2

4
推荐指数
1
解决办法
3385
查看次数

在已使用 fPIC 的情况下创建共享对象时,不能使用针对符号的重定位 R_X86_64_PC32

我看过很多关于解决此类链接器错误的帖子,在大多数情况下,人们只是忘记使用 进行编译-fPIC,有时人们在使用内联函数时遇到麻烦,等等,但这里的情况并非如此。我正在尝试使用Pybind11包装 python 的 c++ 库。在此过程中,我想将一些静态库(其中之一是newmat11)链接到 .so 文件中。

我使用 automake 系统构建 newmat11 库-fPIC(这是一些输出......)

...
g++ -DHAVE_CONFIG_H -I. -g -O2 -MT newmat8.lo -MD -MP -MF .deps/newmat8.Tpo -c newmat8.cpp  -fPIC -DPIC -o newmat8.o
...
ar cru libnewmat11.a  bandmat.o cholesky.o evalue.o fft.o jacobi.o hholder.o myexcept.o newfft.o newmat1.o newmat2.o newmat3.o newmat4.o newmat5.o newmat6.o newmat7.o newmat8.o newmat9.o newmatex.o newmatrm.o solution.o sort.o submat.o svd.o
Run Code Online (Sandbox Code Playgroud)

事实上,当我运行时readelf --relocs libnewmat11.a,我看到很多重新定位的符号。这是给我带来麻烦的一个:

$readelf --relocs libnewmat11.a | grep ZTIN6NEWMAT17Sing
000000001d20  013c00000002 R_X86_64_PC32 …
Run Code Online (Sandbox Code Playgroud)

g++ setuptools linker-errors fpic pybind11

4
推荐指数
1
解决办法
4004
查看次数

使用 NASM 进行 Windows x64 汇编编程中未解析的外部符号 printf

我最近一直在尝试学习汇编,并偶然发现了这篇文章。作者使用NASM和微软链接器搭建汇编环境。我按照相同的步骤安装了 NASM。然后我开始编译 hello world 应用程序。编译成功,但是在链接阶段出现错误。错误如下:

hello_world.obj : error LNK2001: unresolved external symbol printf
hello_world_basic.exe : fatal error LNK1120: 1 unresolved external
Run Code Online (Sandbox Code Playgroud)

这是微软链接器(link.exe)的输出。我按照帖子中的描述从开发人员命令提示符运行链接命令,并且因为 hello world 是一个 64 位应用程序,所以我正确设置了 LIB 环境变量(即使帖子中没有提到)。

hello_world.asm

bits 64
default rel

segment .data
   msg db "Hello world!", 0xd, 0xa, 0

segment .text
global main
extern ExitProcess
extern printf




main:
   push    rbp
   mov     rbp, rsp
   sub     rsp, 32

   lea     rcx, [msg]
   call    printf

   xor     rax, rax
   call    ExitProcess

Run Code Online (Sandbox Code Playgroud)

在 windows 命令提示符下编译程序。

nasm -f win64 -o hello_world.obj …

windows assembly x86-64 nasm linker-errors

4
推荐指数
2
解决办法
1213
查看次数