I am struggling since quite some time with restructuring a c++ project of mine. It has been working when it still was a single file that included every part of that (literally right in the file, not even #include, except for io stuff). It compiled fine then, but now:
I have single files for every class, since compilation is taking several seconds if it is all squished together, which is a bit too long for the usually rather small changes i make to my code. This was working well so far, i made the appropiate Makefile (i am using gcc and make via command line, no ides, on msys2 on windows) and the compilation spits out no code errors, except the problem where it literally does not find a single reference to my functions, or anything else, like the std:: stuff.
While trying to figure out where it comes from, i rebuilt the code structure for something much smaller. Just 2 cpp files, each includes a function definition for a function signature that was included via a short .h file. (e.g. func.cpp:1 #include"func.h"
. test.cpp also contains the 'main' function. i will append the whole code further down in this thread)
Both of these files include only one function(except main, but that is not the point here), which are just a wrapper for simple stdout
stuff. To make those work(compile without code syntax errors), i had to include the appropiate std header in the according cpp file. so func.cpp and test.cpp include func.h and test.h respectively, and BOTH include iostream
.
Now, when i try to compile that, i get the same undefined reference errors i have in my bigger project (though a whole lot less here)
C:\msys64\tmp\cc0oLmKB.o:func.cpp:(.text+0x17): 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:\msys64\tmp\cc0oLmKB.o:func.cpp:(.text+0x26): undefined reference to 'std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
C:\msys64\tmp\cc0oLmKB.o:func.cpp:(.text+0x46): undefined reference to 'std::ios_base::Init::~Init()'
(this goes on for around half a dozen more functions.)
This is apparently a problem with cpp code, since it works fine if i replace the c++-iostream stuff with c's stdio/printf (in both files, so 2xiostream => 2xstdio), but i have absolutely no idead how to fix that. i have wrapped the iostream includes in a ifndef block so that it would not be included more often than it has to(which makes no sense here since none of the cpp files includes the other one, but i am desperate!), but that does not change a thing. (previously, i compiled both into the respective .o file, and linked them via gcc func.o test.o -o main, from which i got the same result, lots of undefined references).
I have absolutely no idea what to do now. My only idea left is, that it might be a problem with the gcc/std-libs version for msys2, so i will try that same piece of code on a linux machine and tell you if that works, but it probably won't.
Looking forward to yours answers, Patrick
I have no idea why this is flagged as a duplicate. the existing threads do not answer my problem. as i said, it works with c header files, but not with c++ ones. this is probably a linking error, but i have yet to see an explanation on how to fix this.
extended infos:
func.h:
int myprint();
test.h:
void stuff();
func.cpp:
#include"func.h"
#ifndef IO
#define IO
#include<iostream>
#endif
//if the iostream stuff is replaced with stdio.h and std::cout etc. replaced with printf, it works
int myprint(){
std::cout<<"hello"<<std::endl;
}
test.cpp:
#include"func.h"
#include"test.h"
#ifndef IO
#define IO
#include<iostream>
#endif
void stuff(){
std::cout<<"stuff"<<std::endl;
}
int main(int argc, char **argv){
myprint();
stuff();
return 0;
}
compile command: (adding any std also did not help, just so you know)
gcc -o main func.cpp test.cpp
compiler output:
C:\msys64\tmp\ccY7n59u.o:func.cpp:(.text+0x17): 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:\msys64\tmp\ccY7n59u.o:func.cpp:(.text+0x26): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
C:\msys64\tmp\ccY7n59u.o:func.cpp:(.text+0x41): undefined reference to `std::ios_base::Init::~Init()'
C:\msys64\tmp\ccY7n59u.o:func.cpp:(.text+0x71): undefined reference to `std::ios_base::Init::Init()'
C:\msys64\tmp\ccY7n59u.o:func.cpp:(.rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_[.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_]+0x0): 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:\msys64\tmp\ccY7n59u.o:func.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): undefined reference to `std::cout'
C:\msys64\tmp\ccSaa3Ax.o:test.cpp:(.text+0x17): 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:\msys64\tmp\ccSaa3Ax.o:test.cpp:(.text+0x26): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
C:\msys64\tmp\ccSaa3Ax.o:test.cpp:(.text+0x6a): undefined reference to `std::ios_base::Init::~Init()'
C:\msys64\tmp\ccSaa3Ax.o:test.cpp:(.text+0x9a): undefined reference to `std::ios_base::Init::Init()'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:2: main] Error 1
gcc -v:
Using built-in specs.
COLLECT_GCC=C:\msys64\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-6.3.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,objc,obj-c++,fortran,ada --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mingw64 --with-pkgversion='Rev3, Built by MSYS2 project' --with-bugurl=https://sourceforge.net/projects/msys2 --with-gnu-as --with-gnu-ld
Thread model: posix
gcc version 6.3.0 (Rev3, Built by MSYS2 project)