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
func.h
func.cpp
obj.h
obj.cpp
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:
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!
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!