2

Config.h:

#ifndef CONFIG_H
#define CONFIG_H
class Config {
 public:
  static int GetDefult();
};
#endif //CONFIG_H

Config.cpp:

#include "Config.h"
int Config::GetDefult(){
 return 1;
}

main.cpp:

#include "Config.h"
#include<iostream>
using namespace std;
int main()
{
  cout << Config::GetDefult() << endl;
}

When my main file is linking, it prompts undefined references:

/tmp/ccD7rrRH.o: In function `main':
main.cpp:(.text+0x5): undefined reference to `Config::GetDefult()'
collect2: error: ld returned 1 exit status

How to include the .h file and make the program run successfully?

Biffen
  • 6,249
  • 6
  • 28
  • 36
q57259028
  • 59
  • 6
  • How do you compile your project? What is your compiler? – VLL Dec 16 '20 at 07:53
  • 1
    Please explain "When my main file is running, it prompts undefined references." If you have linking problems you cannot run it. If you successfully linked at build time but then running the program causes runtime errors on not finding libraries it is a different problem. Please provide the error messages you get, directly here, as text, in full and verbatim. – Yunnosch Dec 16 '20 at 07:53
  • 3
    `#include "Config.cpp"` is outright wrong. – paddy Dec 16 '20 at 07:54
  • 1
    I am not sure how the shown code could cause any of the problems you might mean as far as I can imagine. Please provide a [mre], so that all mentioned identifiers are clarified. If we have to guess that you will have "defined and declared every mentioned thing appropriatly", then looking for linking and definition problems is impossible. – Yunnosch Dec 16 '20 at 07:57
  • Sorry,I revised the question – q57259028 Dec 16 '20 at 08:07
  • Your shown code is still not a MRE. Please turn it into one. Especially the `...` has to go. Make sure that compiling what you show gets there errors you want to discuss. – Yunnosch Dec 16 '20 at 08:14
  • 1
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Biffen Dec 16 '20 at 08:21
  • Please fix the conflict in your shown code between `static Config* GetDefult();` and `int Config::GetDefult()`. Please make a MRE which has only the errors you want to discuss. Prety please. – Yunnosch Dec 16 '20 at 08:29
  • 1
    @q57259028: Please show how your IDE or command line tools are compiling these files. Right now, it looks like your IDE isn't passing both main.cpp and config.cpp to the compiler. – Bill Lynch Dec 16 '20 at 08:39
  • You'll always get that error when linking your main file. That's why you're not supposed to link the main file but you're supposed to link *all* of the files together. – David Schwartz Dec 16 '20 at 08:48
  • @Bill Lynch I use Code::Blocks – q57259028 Dec 16 '20 at 08:57

1 Answers1

1

Playing with the shown code (the more useful one from first versio of your question and after fixing the differences to a MRE with wild guesses),
I got mostly "is not a member of class Config".

I got it to build like below; because obviously you can fix that error by making them members of that class. I chose private because it seems appropriate, but that is not needed for the solution of your problem.

#ifndef CONFIG_H
#define CONFIG_H
#include <mutex>

using namespace std;
class Config {
 public:
  static Config* GetDefult();
 private:
  static Config* p_instance;
  static mutex mutex_;
};
#endif //CONFIG_H
//Config.cpp
#include "Config.h"


Config* Config::GetDefult()
{
    return Config::p_instance; // just to fix building, wild guess
}

Config* Config::p_instance;
mutex Config::mutex_;

However, please do not using namespace std; in a header.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54