I'm trying to implement a library that uses the rather popular C++ header paradigm (separating definition and declaration in a .h / .hpp and a .cpp file). I'm using the MSVC cl.exe compiler to target the Windows operating system.
I explicitly use the #include
directive targeting the header file abc.hpp
where the definition is expressed. I expect the compiler to automatically look for the corresponding abc.cpp
file and redirect calls to abc.hpp::abc()
towards abc.cpp::abc()
.
(I know I could directly #include "abc.cpp"
in the example but remember I'm trying to solve a bigger problem that is include an entire library from it's header files.)
(I also tried to use the /I
compiler option but after both testing and reading the documentation it's not of much use.)
https://learn.microsoft.com/en-us/cpp/build/reference/i-additional-include-directories
I described the test I run bellow and the compiler error I get:
######## main.cpp ########
#include <stdio.h> // printf_s
#include "abc.hpp"
int main(int argc, char *argv[]) {
abc();
return(0);
}
######## abc.hpp ########
void abc();
######## abc.cpp ########
#include "abc.hpp"
void abc() {
printf("ABC!\n");
}
######## command line ########
> cl main.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.X for x64
Copyright (C) Microsoft Corporation. All rights reserved.
main.cpp
Microsoft (R) Incremental Linker Version 14.X
Copyright (C) Microsoft Corporation. All rights reserved.
/out:main.exe
main.obj
main.obj : error LNK2019: unresolved external symbol "void __cdecl abc(void)" (?abc@@YAXXZ) referenced in function main
main.exe : fatal error LNK1120: 1 unresolved externals
Is my expectation about the compiler automatically remapping abc.hpp::abc()
towards abc.cpp::abc()
off the mark?
Am I forced to have the .cpp
code be compiler as a separate .lib
to be able to /link /LIBPATH:
it to the compiler?
I'd be pleased to hear your insight about this matter.