c++ - arm-none-eabi-gcc 在 C : undefined references 中使用 C++ 代码

标签 c++ c embedded toolchain

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

主.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_ */

杂项.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

这是我获得的 undefined reference :

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

我在这里错过了什么?

最佳答案

您正在链接 CC,您必须链接 CXX - 它不仅是您也不应该向 添加每行参数CFLAGS, CXXFLAGS.

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++ 担心这个更容易。

关于c++ - arm-none-eabi-gcc 在 C : undefined references 中使用 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53775780/

相关文章:

c# - 将现有的 QWidget 停靠在 QWindow 中

c++ - 模板参数类型被编译器视为完整的,但其定义尚不可见

c# - 将具有未知大小数组的结构从非托管代码传递到托管代码

c - zmq_ctx_term() 在套接字关闭时阻塞

c++ - 我是不是遗漏了什么,或者虚拟电话的表现不如人们想象的那么糟糕

c++ - gcc 用于解析代码

c - 尝试在整数数组中复制命令行参数时出现段错误

c - 我的 LCM 程序的复杂性是多少?

在C中将有符号整数转换为无符号整数

c - AT91sam7x 256 : Interrupt just run once