-1

When I run my C++ compiler in Visual Studio code without any classes or declarations from other files it compiles the code perfectly. But whenever I try to run the file with

#include "class.h"

The compiler doesn't work and it doesn't compile the files properly.

Undefined symbols for architecture x86_64:
  "Pelaaja::Pelaaja(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >)", referenced from:
      luo_pelaajat_oliot(std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > >) in main-ef50f2.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here above is one error code from a file that I try to compile. The file uses pointers to a class that is defined in another file. I'm using macOS, and when I try the same code on a Linux system it compiles it perfectly. What could be the problem here?

  • The syntax for including headers in C++ is `#include "class.h"` and `#include `, not `#include class.h`. – kotatsuyaki May 29 '22 at 09:26
  • `#include class.h` is not an include conforming to the standard; you need to use: `#include ` or `#include "class.h"` I assume this is not in your actual code. The error you observe is caused by not linking with the object file generated from the implementation file corresponding to `class.h` or the implementation missing in that file. – fabian May 29 '22 at 09:26
  • Sorry there was a typo @kotatsuyaki, it is fixed now. – valtterihaav May 29 '22 at 09:31
  • How do I link them properly? From terminal? @fabian – valtterihaav May 29 '22 at 09:32
  • 1
    As you included a header (`class.h`) you need at least to provide the implementation of all its declaration at link time. How did you link? What are the libraries/object files? – Jean-Baptiste Yunès May 29 '22 at 09:56

0 Answers0