Answer the question
In order to leave comments, you need to log in
Why does the error LNK2001 Unresolved external character occur?
Main project file:
#include <iostream>
#include "unit1.h"
using namespace std;
int main()
{
XReset();
MyType Y;
Y = X + 3;
cout << "Y=" << Y << endl;
cin;
return 0;
}
#pragma once
#include <cstdlib>
typedef int MyType;
extern MyType X;
void XReset()
{
X = rand() % 10;
}
Answer the question
In order to leave comments, you need to log in
As they say, "either take off the cross, or put on underpants." And learn the concept of "compilation unit".
What is the structure of your project? "One compilation unit" or "many compilation units"?
C is not far from assemblers. And in assemblers, the program was compiled in parts and put together by a linker (linker, linker) - in those days there was a lot of code, but little data. Many of the errors could not be identified without running the linker. C++ takes advantage of many of the architectural features of assemblers and C—at least none of the modular solutions have become a recommendation (other than the crutch extern template class
).
But how to say “there is a variable / function, of such and such a type and in another compilation unit”? There are function prototypes and extern variable definitions for this. They are usually included in header files with the requirement that nothing in the header file should produce code. And the code is produced…
• Global variables (without typedef, extern).
• Non-template functions (except inline).
• Fully specialized template functions (except inline).
• Command "specialize template" (template class).
However…
• Functions in the body of a struct/class are automatically inline and do not produce code.
• For implicit template specialization, there are bypasses - the code is generated twice, but does not produce an error.
• The custom header is usually included first to make sure it doesn't have any missing dependencies.
In the "one compilation unit" system, everything is simple: there is exactly one file to be compiled. Then the header files may well contain constructs that produce code.
The "single compilation unit" and "many compilation units" systems can be combined, but you need to know:
• All headers that produce code must be connected from a single compilation unit. It is necessary to clearly understand from which one, and not to connect from strangers.
• The library must still have a header facade that does not produce code and is designed to interface with other compilation units.
This construction speeds up complete recompilation and is often used for libraries, but you need to know: huge libraries like SqLite, in 5 megabytes of preprocessed code, interfere with the parallelization of compilation (because while SqLite is being compiled, the rest of the processors will quite build the rest of the program).
// В схеме «одна единица компиляции»: ничего не делать.
// В схеме «много единиц компиляции»: лишняя зависимость; унести в unit1.cpp
#include <cstdlib>
// В схеме «одна единица компиляции»: убрать extern.
// В схеме «много единиц компиляции»: завести unit1.cpp, там сделать MyType x;
extern MyType X;
// В схеме «одна единица компиляции»: ничего не делать.
// В схеме «много единиц компиляции»: перенести функцию в unit1.cpp, оставив в .h только прототип.
void XReset() {}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question