Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (monk12, Quad), 830 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Working with header files and linker #486798
10/11/22 17:59
10/11/22 17:59
Joined: Jan 2022
Posts: 57
N
NorbertSz Offline OP
Junior Member
NorbertSz  Offline OP
Junior Member
N

Joined: Jan 2022
Posts: 57
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!

Last edited by NorbertSz; 10/11/22 18:46.
Re: Working with header files and linker [Re: NorbertSz] #486802
10/12/22 11:55
10/12/22 11:55
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
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.

Re: Working with header files and linker [Re: jcl] #486825
10/17/22 15:34
10/17/22 15:34
Joined: Jan 2022
Posts: 57
N
NorbertSz Offline OP
Junior Member
NorbertSz  Offline OP
Junior Member
N

Joined: Jan 2022
Posts: 57
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.

Last edited by NorbertSz; 10/17/22 15:35.

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1