【问题标题】:arm-none-eabi-gcc using C++ code in C: undefined referencesarm-none-eabi-gcc 在 C 中使用 C++ 代码:未定义的引用
【发布时间】:2019-05-15 11:48:06
【问题描述】:

我需要在我的 C 程序中使用一些 C++ 代码,这些代码必须与 arm-none-eabi 工具链交叉编译。我首先尝试以下简单的程序:

main.c

#include "misc.h"

int main(){
    say_hello();
    return 0;
}

杂项.h

#ifndef _MISC_H_
#define _MISC_H_

/**
 * \brief Prints hello to stdout
 */
void say_hello();

#endif /* _MISC_H_ */

misc.c

/* C++ implementation */
#include <iostream>

using namespace std;

void cpp_say_hello(){
    cout << "Hello world!" << endl;
}

/* C wrapper */
extern "C"{
    #include "misc.h"

    void say_hello(){
        cpp_say_hello();
    }
}

生成文件

CFLAGS += -lstdc++
CXXFLAGS += -c

hello_world_mix: misc.o main.c
    $(CC) $(CFLAGS) $^ -o $@

misc.o: misc.cpp
    $(CXX) $(CXXFLAGS) $^ -o $@

clean:
    rm -f *.o
    rm -f *~

当我本地编译它时(通过简单地做'make')它工作得很好。但是,如果我尝试使用以下代码进行交叉编译:

CC=arm-none-eabi-gcc CXX=arm-none-eabi-g++ CFLAGS=--specs=nosys.specs make

这是我获得的未定义引用:

arm-none-eabi-gcc --specs=nosys.specs -lstdc++ misc.o main.c -o hello_world_mix
/usr/lib/gcc/arm-none-eabi/8.2.0/../../../../arm-none-eabi/bin/ld: misc.o: in function `cpp_say_hello()':
misc.cpp:(.text+0x34): 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*)'
/usr/lib/gcc/arm-none-eabi/8.2.0/../../../../arm-none-eabi/bin/ld: misc.cpp:(.text+0x44): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/lib/gcc/arm-none-eabi/8.2.0/../../../../arm-none-eabi/bin/ld: misc.cpp:(.text+0x5c): undefined reference to `std::cout'
/usr/lib/gcc/arm-none-eabi/8.2.0/../../../../arm-none-eabi/bin/ld: misc.cpp:(.text+0x60): 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> >&)'
/usr/lib/gcc/arm-none-eabi/8.2.0/../../../../arm-none-eabi/bin/ld: misc.o: in function `__static_initialization_and_destruction_0(int, int)':
misc.cpp:(.text+0xb4): undefined reference to `std::ios_base::Init::Init()'
/usr/lib/gcc/arm-none-eabi/8.2.0/../../../../arm-none-eabi/bin/ld: misc.cpp:(.text+0xe4): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:5: hello_world_mix] Error 1

我在这里错过了什么?

【问题讨论】:

  • 库的顺序很重要。如果源文件或目标文件A 依赖于库B,那么您必须与A 链接first。或者让您自己更轻松地使用g++ 进行链接。

标签: c++ c embedded toolchain


【解决方案1】:

您正在与 CC 链接,您必须与 CXX 链接 - 这不仅是您不应该向 CFLAGSCXXFLAGS 添加每行参数。

hello_world_mix: misc.o main.o
    $(CXX) $(CFLAGS) $^ -o $@

main.o: main.c
    $(CC) -c $(CFLAGS) $^ -o $@

misc.o: misc.cpp
    $(CXX) -c $(CXXFLAGS) $^ -o $@

clean:
    rm -f *.o
    rm -f *~

原因很可能是 -l 选项的顺序在 some GCC versions and not in others. 中可能很重要 - 但让 g++ 更容易担心这一点。

【讨论】:

  • 感谢您的回复。抱歉,如果我必须与 g++ 链接,make 文件不应该是这样的:pastebin.com/raw/X4HgVhQG ?
  • @wellsaid 哎呀,已修复。
  • 好的,谢谢。现在它适用于本机但对于 arm-none-eabi 我获得了其他未定义的引用:pastebin.com/raw/3iRCg5ZA。但也许这与问题无关
  • 有趣。我想知道您的 libstdc++ 是否与标头同步!
  • 我已经成功地用这个命令远程了那些未定义的引用:make CC=arm-none-eabi-gcc CXX=arm-none-eabi-g++ CXXFLAGS="-specs=nosys.specs -mcpu=cortex-m3"
猜你喜欢
  • 2017-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-03
相关资源
最近更新 更多