Gamestudio Links
Zorro Links
Newest Posts
Miliseconds capturing
by Dion. 02/06/23 13:27
Weekly Pivots
by paulk. 02/03/23 18:02
Corvitextu texture browser released...
by NeoDumont. 02/02/23 22:35
How many markets with Zorro free?
by XRaf. 02/02/23 15:33
Experiencing Issues with 2.53.6
by vinitrinh. 02/02/23 10:07
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
3 registered members (Michael_Schwarz, qin, AndrewAMD), 559 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Puezzin, Quant2, qin, ktroch, dagliottone
18890 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: 44
N
NorbertSz Offline OP
Newbie
NorbertSz  Offline OP
Newbie
N

Joined: Jan 2022
Posts: 44
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,933
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,933
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: 44
N
NorbertSz Offline OP
Newbie
NorbertSz  Offline OP
Newbie
N

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