0

I have a starter project and I need to write a custom allocator and diagnostic tools for it. I made a class Class in which I have 2 methods for the custom allocator void alloc() void dealloc() and for the diagnostic tools void evaluate().
Now, I declared an object test of type Class in CustomAllocator.h and use the 2 methods to allocate and deallocate memory with no problems. But when I try to call the evaluate() method in CustomAllocatorTest.cpp I got the linker error class Class test(?test@@3VClass@@A) already defined in CustomAllocatorTest.obj and LNK1169 one or more multiply defined symbols found.

Class.h

#pragma once
class Class
{
public:
    void alloc() { std::cout << "alloc"; }
    void dealloc() { std::cout << "dealloc"; }
    void evaluate() { std::cout << "evaluate"; }
};

CustomAllocator.h

#ifndef _CUSTOM_ALLOCATOR_H_
#define _CUSTOM_ALLOCATOR_H_

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <stdlib.h>

#include "Class.h"
Class test;

#endif  // _CUSTOM_ALLOCATOR_H_

CustomAllocator.cpp (#include "stdafx.h" includes "CustomAllocator.h")

#include "stdafx.h"

using namespace std;

int main()
{
  test.evaluate();
  return 0;
}
Ovidiu Firescu
  • 385
  • 3
  • 11
  • At StackOverflow the code and errors need to be part of the question and must be in text. The main purpose of a question is to help future readers with the same problem years from now. – drescherjm Mar 16 '20 at 12:34
  • I understand I will try to edit it and add all the classes but don't know if it will help future readers since I have pretty specific things in the question. – Ovidiu Firescu Mar 16 '20 at 13:50

1 Answers1

1

in your file CustomAllocator.h you declare test in global scope :

#ifndef _CUSTOM_ALLOCATOR_H_
#define _CUSTOM_ALLOCATOR_H_
#include "Class.h"

Class test; // <-- Declaration of test 

#endif 

But your CustomAllocator.h is included many times, in many places (in UseCustomAllocator.h and CustomAllocator.cpp) which will generate an already defined error for test variable.

Please see how to declare externe globale variable here #pragma once doesn't prevent multiple variable definitions

Landstalker
  • 1,368
  • 8
  • 9
  • Ah, I understand so `Class test;` will get declared many times in a lot of files. I tried to follow advice in the link and declare the `Class test;` in the `CustomAllocator.cpp` and `extern Class test;` in `CustomAllocator.h` and now I get the following error `unresolved external symbol "class Class test"(?test@@3VClass@@A)`. The object should be included only once and seen in the other files since the `CustomAllocator.h` is included. – Ovidiu Firescu Mar 16 '20 at 14:00
  • if you `extern Class test;` in a header you need `Class test;` in some translation unit (source file). – drescherjm Mar 16 '20 at 14:33
  • @OvidiuFirescu as explained by @drescherjm, you must write `Class test;` (without keyword `extern`) in a `CPP` file. You can do it in your `CustomAllocatorTest.cpp` for exemple. and in `CustomAllocator.h` you write `extern Class test;` hope this will help you – Landstalker Mar 16 '20 at 14:41
  • That's what I'm saying I used `extern Class text;` in the header `CustomAllocator.h` and `Class test;` in `CustomAllocator.cpp` and I get the above error. Tried doing what Landstalker said which is pretty similar to what I did and got the same above error. – Ovidiu Firescu Mar 16 '20 at 15:06
  • If you get the same error do a clean build. Files compiled before the change may have not been recompiled after. – drescherjm Mar 16 '20 at 16:20
  • @OvidiuFirescu I did the same test now, and it works for me. – Landstalker Mar 16 '20 at 16:22
  • I don't understand why, but I keep getting the error, I can't get rid of it. I even downloaded again the project that I posted on github and did the changes with `extern Class text;` to the `CustomAllocator.h` and `Class test;` to `CustomAllocator.cpp` and then compiled it and still get the same thing. Any idea what I might be doing wrong ? Don't know if it's relevant but I'm using VS19. – Ovidiu Firescu Mar 16 '20 at 16:58
  • 1
    ***Any idea what I might be doing wrong ?*** No it should work. Unless you are creating a dll. If the library is a dll you are not handling the export / import at all. – drescherjm Mar 16 '20 at 17:52
  • What's the problem if it's a dll? How should I handle it so it works? I'm only supposed to write the custom allocator and the diagnostics tools, I don't really know about handling the export/import – Ovidiu Firescu Mar 16 '20 at 17:56
  • @OvidiuFirescu, try to download project, extract it, create a NEW EXEC project in visual studio, copy .h and .cpp of GIT project to new project folder, Add all copied files : add existing items in Vs option, add extern declaration, and see if OK – Landstalker Mar 16 '20 at 19:24
  • 1
    [https://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport](https://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport) – drescherjm Mar 16 '20 at 19:25
  • I finally managed to understand at least a little better the .dll files and how to handle the export functions. I saw there are several methods, managed to do it by making an EXPORTS statement in the DEF file). I still have so much more to learn and study... Thanks a lot to both of you for spending so much time helping me and it really solved my problem. I really appreciate your help, I will edit the question when I've got time and add the code. – Ovidiu Firescu Mar 16 '20 at 20:31