In a static library project, I have a header file with declared but not implemented functions.
I have a .cpp file that implements these functions.
Then, to better understand linker errors, I copied the cpp file so I have an exact duplicate of it that also gets compiled. So, both files have double implementations for every symbol in the header.
It compiles, and when used in another project, it links.
Here a minimum example for static library:
api.hpp:
void printWhatever();
errortest.cpp and duplicate.cpp are identical:
#include "api.hpp"
#include <iostream>
void printWhatever(){
std::cout << "hi " << "n";
}
I compile this as a static library with these 2 source files. I see the compiler produce reports for both files.
Now I use this compiled library in an executable, a different project: main.cpp:
#include <api.hpp>
int main(int argc, const char * argv[]) {
printWhatever();
return 0;
}
It runs and prints "hi".
Why is there not a multiple definition for the function?
Aucun commentaire:
Enregistrer un commentaire