标准库与 C++ 文件的链接问题

问题描述 投票:0回答:0

我有一个包含一堆 C 文件和一个 C++ 文件的项目。有这个

app.cpp
文件和
main.c
从中调用
App()
的地方。

#include <iostream>
#include "app.h"

int App(void)
{
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

里面

app.h
我只有函数声明。

#ifndef APP_H
#define APP_H

#include "main.h"

#ifdef __cplusplus
extern "C" {
#endif

int App(void);

#ifdef __cplusplus
}
#endif

#endif

当我尝试构建我的项目文件时出现以下错误:

undefined reference to `std::ios_base::Init::~Init()'

这是一个链接器问题。我搜索了这个问题,我发现了两个主要原因:

  • 添加
    -lstdc++
    到链接器选项
  • 确保使用
    g++
    编译器而不是
    gcc

我都用过,但仍然出现此错误。请注意,我使用的不是 GNU g++ 编译器,而是 ARM g++ 编译器。我正在使用 this Makefile 来构建我的项目。编译是在 Windows 上完成的。

有谁知道这里可能是什么问题或如何解决这个问题?

编辑:

我正在使用 stm32-to-vscode 扩展,它可能会在某种程度上干扰构建过程(否则用于从 VS Code 构建、调试和闪存代码到 STM 设备)。

我无法再重现如上所述的确切链接器错误,但这是完成链接的整个日志(来自 Makefile)。请注意,编译过程不会导致任何错误,因此为了方便起见,我只显示链接器日志。

c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard\libc_nano.a(libc_a-closer.o): in function `_close_r':
closer.c:(.text._close_r+0xc): warning: _close is not implemented and will always fail
c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard\libc_nano.a(libc_a-lseekr.o): in function `_lseek_r':
lseekr.c:(.text._lseek_r+0x10): warning: _lseek is not implemented and will always fail
c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard\libc_nano.a(libc_a-readr.o): in function `_read_r':
readr.c:(.text._read_r+0x10): warning: _read is not implemented and will always fail
c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard\libc_nano.a(libc_a-writer.o): in function `_write_r':
writer.c:(.text._write_r+0x10): warning: _write is not implemented and will always fail
c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: warning: build/NucleoTest.elf has a LOAD segment with RWX permissions
c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: build/app.o: in function `__static_initialization_and_destruction_0(int, int)':
c:\users\luka\appdata\roaming\code\user\globalstorage\bmd.stm32-for-vscode\@xpack-dev-tools\arm-none-eabi-gcc\12.2.1-1.2.1\.content\arm-none-eabi\include\c++\12.2.1/iostream:74: undefined reference to `std::ios_base::Init::Init()'
c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: build/app.o: in function `__static_initialization_and_destruction_0(int, int)':
C:\Repository\Code\STM32Cube\NucleoTest/Core/Src/app.cpp:8: undefined reference to `std::ios_base::Init::~Init()'
c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: build/app.o: in function `App':      
C:\Repository\Code\STM32Cube\NucleoTest/Core/Src/app.cpp:6: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: build/app.o: in function `std::ostream::operator<<(std::ostream& (*)(std::ostream&))':
c:\users\luka\appdata\roaming\code\user\globalstorage\bmd.stm32-for-vscode\@xpack-dev-tools\arm-none-eabi-gcc\12.2.1-1.2.1\.content\arm-none-eabi\include\c++\12.2.1/ostream:113: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: build/app.o: in function `App':
C:\Repository\Code\STM32Cube\NucleoTest/Core/Src/app.cpp:8: undefined reference to `std::cout'
c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard\libc_nano.a(libc_a-signalr.o): in function `_kill_r':
signalr.c:(.text._kill_r+0xe): undefined reference to `_kill'
c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/bin/ld.exe: c:/users/luka/appdata/roaming/code/user/globalstorage/bmd.stm32-for-vscode/@xpack-dev-tools/arm-none-eabi-gcc/12.2.1-1.2.1/.content/bin/../lib/gcc/arm-none-eabi/12.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard\libc_nano.a(libc_a-signalr.o): in function `_getpid_r':
signalr.c:(.text._getpid_r+0x0): undefined reference to `_getpid'
collect2.exe: error: ld returned 1 exit status
MAKE.EXE: *** [STM32Make.make:227: build/NucleoTest.elf] Error 1
c++ makefile stl arm cross-compiling
© www.soinside.com 2019 - 2024. All rights reserved.