2

I am trying to port some code to Linux which is originally written in Microsoft Visual Studio. The code uses std::wstring and when I try to compile, I get the following error:

undefined reference to `myFunc(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&)'

which is declared as

myfunc(const std::wstring &str)

Is there some additional header file I need to include?

peterh
  • 11,875
  • 18
  • 85
  • 108
Jon Helt-Hansen
  • 383
  • 1
  • 5
  • 19
  • Have you included correct header and `wstring` should be under std namespace. – billz Sep 29 '14 at 09:19
  • 3
    Are you sure you are compiling and linking in the definition for the function `myfunc(const std::wstring &str)`? – Niall Sep 29 '14 at 09:20
  • So far this doesnt' seem like an issue with gcc. It's complaining that it can't find one of your own functions. Where have you defined `myFunc` ? How do you compile and link your application ? – nos Sep 29 '14 at 09:21
  • @billz: yes small typo here - sorry for that. is included – Jon Helt-Hansen Sep 29 '14 at 09:22
  • `myFunc()` is defined in the same directory as the function calling it. I dont have much experience with wstring but as far as I can tell from the above error the std::wstring is translated to std::basic_string<...> (??) – Jon Helt-Hansen Sep 29 '14 at 09:29
  • @JonHelt-Hansen, the error doesn't seem related to the `wstring`, the compiler is complaining about the function `myFunc` and not a method on `wstring`. – Niall Sep 29 '14 at 09:31
  • @JonHelt-Hansen So does that mean it is defined in another file ? The normal way to build a C++ program is to compile each source code file to an object file, and then link together all object files to produce an executable. The error you get here suggest you're not doing that. std::wstring is indeed a specialization of basic_string, but that's beside the issue you have. What are you doing ? Which commands are your running when you get that error ? – nos Sep 29 '14 at 09:32
  • I am using Qt. The files are linked together using Makefile. I only have one definition of `myFunc()` which takes `std::wstring` as input. I am a bit puzzled about the error I get but my assumption so far, was that is is related to the internal handling of `std::wstring` (but of course I could be very wrong about this). As said, this is code originally written in VS and is working on Windows platform. – Jon Helt-Hansen Sep 29 '14 at 09:38
  • Did you go through everything listed in http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix ? – eerorika Sep 29 '14 at 09:59
  • @JonHelt-Hansen As seen e.g. [here](http://msdn.microsoft.com/en-us/library/wt3s3k55.aspx) wstring is just a typedef. So the fact that you see basic_string in there, is perfectly normal and fine. The issue is more that the linker cannot find the `myFunc` function. There can be many reasons for that e.g. the file where it's defined is not being compiled or linked in., it's in a different namespace, you've misspelled the function name somewhere etc.. – nos Sep 29 '14 at 10:24
  • And, if you're simply moving an existing project from linux to windows, be aware that linux unlike windows has case sensitive file names, if you're using a Qt creator project, make sure the project doesn't reference a file that has a different case than what is on disk. – nos Sep 29 '14 at 10:26
  • @nos: Yeah, know about the case sensitivity issue - spend quite some time updating includes due to this :) – Jon Helt-Hansen Sep 29 '14 at 10:55
  • Then someghow the linking must go wrong. From Qt creator when I include the proper header file I am able to find the function using (ctrl-space -> autocompletion), so I'd assume everything should be in order, but apparently not... – Jon Helt-Hansen Sep 29 '14 at 10:57
  • From question I can see that you are declaring myfunc and error is about myFunc. Is it a typo in question, or you don't know that C++ is case-sensitive? – Konstantin Vladimirov Sep 29 '14 at 19:09

1 Answers1

2

Thanks a lot for your help. It did point me in the right direction and unfortunately I am starting to turn blind. Even though I looked a couple of hours of the code in question I kept overlooking that a namespace was missing in the implementation :( Sorry for the inconvience.

peterh
  • 11,875
  • 18
  • 85
  • 108
Jon Helt-Hansen
  • 383
  • 1
  • 5
  • 19