Working with header files and linker

Posted By: NorbertSz

Working with header files and linker - 10/11/22 17:59

Hello!

As far as I know, C++ compiler normally compiles the .cpp files to object files (.o), then the linker can connect everything by the header (.h) files. For simplicity, I have the following files in my Strategy folder. (In the Source/VC++/compile.bat file I added the main Zorro folder, so it can see the files I include.)

cpptest.cpp

Code
#include "func.h"
#include <zorro.h>

DLLFUNC void main(){
    justafunction();
}


func.h

Code
#pragma once

void justafunction();


func.cpp

Code
#include "obj.h"

void justafunction(){
   Something a;
   a.asd();
}


obj.h

Code
#pragma once

class Something{
 public:
  void hello();
  void asd();
};


obj.cpp

Code
#include "obj.h"
#include "func.h"

void Something::hello(){justafunction();}
void Something::asd(){printf("called asd");}


This is a demonstration of cross-linking: I am using a class in a function, but that class also using that function. If I wouldn't use header files, only the cpp, I wouldn't determine the include order, because if I start with the func.cpp, it will fail: it needs the obj.cpp. (I want to separate my huge code to smaller files, that's why I need all this.) It works fine in a simple Visual Studio console application, but compiling from Zorro, the compiler is not findig the definitions. The error of the above codes are the following when I try to run cpptest.cpp with Zorro:

Code
cpptest.cpp compiling..
cpptest.cpp

ZorroDLL.cpp

Generating Code...

   Creating library Cache\Imp.lib and object Cache\Imp.exp

cpptest.obj : error LNK2019: unresolved external symbol "void __cdecl justafunction(void)" (?justafunction@@YAXXZ) referenced in function _main

myStrategy\cpptest.dll : fatal error LNK1120: 1 unresolved externals

Error 061: Can't compile cpptest.dll



It looks like for me that the compiler did not build the .o files. Do you have any suggestions how can I do that automatically from Zorro UI (I mean when I compile the script normally, clicking on Test/Trade)?

Thank you very much!
Posted By: jcl

Re: Working with header files and linker - 10/12/22 11:55

You have forgotten to compile and link func.cpp to the DLL. A linker won't process .cpp or .h files. It's for .obj files only.
Posted By: NorbertSz

Re: Working with header files and linker - 10/17/22 15:34

Okay, thank you, now it's clear. The Visual Studio solutions has Header files and Resource files folder, and does this process automatically. But I need to do that manually in this case, or modify the compile.bat / compile64.bat to do that.

To still though enforce it automatically, the not-elegant and the very-non-standard solution is writing the declarations and definitions in different files, then include them independently from each other.

We have the
- func_declaration.cpp
- func_definition.cpp
- obj_declaration.cpp
- obj_definition.cpp

Then:
in func_definition.cpp I start with including the func_declaration.cpp and obj_declaration.cpp, then obj_definition.cpp.
in obj_definition.cpp I also start with including the func_declaration.cpp and obj_declaration.cpp, then func_definition.cpp.
Every file with the #pragma once command.

Therefore it automatically compiles everything with just one click in Zorro UI, without modifying compile.bat, and I can also separate the code parts. But as I see it would be much cleaner to compile the object files then link them together.
© 2024 lite-C Forums