Reputation: 53
i need to use some C++ code in my C program which has to be cross-compiled with the arm-none-eabi toolchain. I am trying the following simple program first:
#include "misc.h"
int main(){
say_hello();
return 0;
}
#ifndef _MISC_H_
#define _MISC_H_
/**
* \brief Prints hello to stdout
*/
void say_hello();
#endif /* _MISC_H_ */
/* 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 *~
When i compile it natively (by simply doing 'make') it works just fine. But if i try to cross-compile it with:
CC=arm-none-eabi-gcc CXX=arm-none-eabi-g++ CFLAGS=--specs=nosys.specs make
This is the undefined references i obtain:
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
What am i missing here?
Upvotes: 3
Views: 3392
Reputation: 133909
You're linking with CC
, you must link with CXX
- it is not only the Also you shouldn't add per-line arguments to the 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 *~
The reason might very well be that the ordering of -l
options might be significant in some GCC versions and not in others. - but it is easier to let g++ worry about that.
Upvotes: 2