About Monkey 2 › Forums › Monkey 2 Programming Help › Error importing .h
This topic contains 7 replies, has 2 voices, and was last updated by 
 abakobo
 2 years, 2 months ago.
- 
		AuthorPosts
 - 
		
			
				
February 13, 2017 at 3:28 am #7125
I’ve created a test.h file with nothing but
C++1void test(){}And If I try to import it whit this Monkey 2 code
Monkey12345678910#Import "<mojo>"#Import "test.h"Using mojo..Class MyWindow Extends WindowEndFunction Main()EndI get lots of errors.
But if I remoteMonkey123Class MyWindow Extends WindowEndIt works fine!
What am I doing wrong?
February 13, 2017 at 5:38 pm #7135That’s strange!
Probably an mx2 duplicate def bug.
Or we are always supposed to use .h for definition and .cpp for implementation when using mojo/gui or use static.
With console apps I never had the problem.
Tested with static before the function in .h and works ok
Tested with def in .h and implementation in .cpp/.cand works okIf you want to see some little example I made to learn externs: https://github.com/abakobo/learn_monkey2
It’s messy and not commented but simple. Will later use these as a base for tutos/samplesFebruary 13, 2017 at 6:10 pm #7136I can’t figure out a solution to this…
I’m stuck at this first step right now. :/February 13, 2017 at 7:30 pm #7139Your problem here is that you give the implementation ({}) of your function in the .h file. It is ok to do that in c/c++ rules but it is considered BAD practice and nearly nobody does it. So I suppose Mark has not made mx2 compatible with it and sometimes (when using mojo for example) create duplicates of the same header for some internal mx2 reasons.
What’s GOOD practise?
-put static before your function if the implementation is in the .h file (ie: the{}). This will skip duplicates.[/crayon]Monkey123[crayon-5cba8795bb3a5516569141 inline="true" ]static void test(){};Or
-put your implementation in a.c/.cpp file Edit:could not make it work with a .c file!
.h[/crayon]Monkey123[crayon-5cba8795bb3ab875570610 inline="true" ]void Test(void);.cpp
[/crayon]Monkey123[crayon-5cba8795bb3af569084687 inline="true" ]void Test(void){};and import the
.c/.cpp file along with the .h file in your .monkey2 filedon’t forget to put the ; at end of lines. Sometimes it’s not mandatory but it’ll avoid you error messages.. (in the second case if you don’t put ; in the .h file it will fail to compile)
for better practice use the #ifndef in your .h file
[/crayon]Monkey12345678[crayon-5cba8795bb3b4332902045 inline="true" ]#ifndef TEST_H#define TEST_Hput your declarations here#endifbut here of course test is a bit too simple for a def name!
February 13, 2017 at 7:46 pm #7140Thanks abakobo. I’m still a bit confused though (Cause I’m no good at C nor C++)
How would I structure this code into its own .h and .cpp files then?Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119#ifdef _WIN32#include <windows.h>#else#include <sys/mman.h>#include <fcntl.h> /* For O_* constants */#endif // _WIN32struct LinkedMem {#ifdef _WIN32UINT32 uiVersion;DWORD uiTick;#elseuint32_t uiVersion;uint32_t uiTick;#endiffloat fAvatarPosition[3];float fAvatarFront[3];float fAvatarTop[3];wchar_t name[256];float fCameraPosition[3];float fCameraFront[3];float fCameraTop[3];wchar_t identity[256];#ifdef _WIN32UINT32 context_len;#elseuint32_t context_len;#endifunsigned char context[256];wchar_t description[2048];};LinkedMem *lm = NULL;void initMumble() {#ifdef _WIN32HANDLE hMapObject = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, L"MumbleLink");if (hMapObject == NULL)return;lm = (LinkedMem *) MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkedMem));if (lm == NULL) {CloseHandle(hMapObject);hMapObject = NULL;return;}#elsechar memname[256];snprintf(memname, 256, "/MumbleLink.%d", getuid());int shmfd = shm_open(memname, O_RDWR, S_IRUSR | S_IWUSR);if (shmfd < 0) {return;}lm = (LinkedMem *)(mmap(NULL, sizeof(struct LinkedMem), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd,0));if (lm == (void *)(-1)) {lm = NULL;return;}#endif}void updateMumble() {if (! lm)return;if(lm->uiVersion != 2) {wcsncpy(lm->name, L"TestLink", 256);wcsncpy(lm->description, L"TestLink is a test of the Link plugin.", 2048);lm->uiVersion = 2;}lm->uiTick++;// Left handed coordinate system.// X positive towards "right".// Y positive towards "up".// Z positive towards "front".//// 1 unit = 1 meter// Unit vector pointing out of the avatar's eyes aka "At"-vector.lm->fAvatarFront[0] = 0.0f;lm->fAvatarFront[1] = 0.0f;lm->fAvatarFront[2] = 1.0f;// Unit vector pointing out of the top of the avatar's head aka "Up"-vector (here Top points straight up).lm->fAvatarTop[0] = 0.0f;lm->fAvatarTop[1] = 1.0f;lm->fAvatarTop[2] = 0.0f;// Position of the avatar (here standing slightly off the origin)lm->fAvatarPosition[0] = 0.001f;lm->fAvatarPosition[1] = 0.0f;lm->fAvatarPosition[2] = 0.5f;// Same as avatar but for the camera.lm->fCameraPosition[0] = 0.0f;lm->fCameraPosition[1] = 0.0f;lm->fCameraPosition[2] = 0.0f;lm->fCameraFront[0] = 0.0f;lm->fCameraFront[1] = 0.0f;lm->fCameraFront[2] = 1.0f;lm->fCameraTop[0] = 0.0f;lm->fCameraTop[1] = 1.0f;lm->fCameraTop[2] = 0.0f;// Identifier which uniquely identifies a certain player in a context (e.g. the ingame name).wcsncpy(lm->identity, L"Unique ID", 256);// Context should be equal for players which should be able to hear each other positional and// differ for those who shouldn't (e.g. it could contain the server+port and team)memcpy(lm->context, "ContextBlob\x00\x01\x02\x03\x04", 16);lm->context_len = 16;}February 13, 2017 at 8:11 pm #7141try this without splitting (much easier and there’s something I didn’t get with the split with a .c file!)
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119[crayon-5cba8795c721e441398914 inline="true" ]#ifdef _WIN32#include <windows.h>#else#include <sys/mman.h>#include <fcntl.h> /* For O_* constants */#endif // _WIN32struct LinkedMem {#ifdef _WIN32UINT32 uiVersion;DWORD uiTick;#elseuint32_t uiVersion;uint32_t uiTick;#endiffloat fAvatarPosition[3];float fAvatarFront[3];float fAvatarTop[3];wchar_t name[256];float fCameraPosition[3];float fCameraFront[3];float fCameraTop[3];wchar_t identity[256];#ifdef _WIN32UINT32 context_len;#elseuint32_t context_len;#endifunsigned char context[256];wchar_t description[2048];};LinkedMem *lm = NULL;static void initMumble() {#ifdef _WIN32HANDLE hMapObject = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, L"MumbleLink");if (hMapObject == NULL)return;lm = (LinkedMem *) MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkedMem));if (lm == NULL) {CloseHandle(hMapObject);hMapObject = NULL;return;}#elsechar memname[256];snprintf(memname, 256, "/MumbleLink.%d", getuid());int shmfd = shm_open(memname, O_RDWR, S_IRUSR | S_IWUSR);if (shmfd < 0) {return;}lm = (LinkedMem *)(mmap(NULL, sizeof(struct LinkedMem), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd,0));if (lm == (void *)(-1)) {lm = NULL;return;}#endif}static void updateMumble() {if (! lm)return;if(lm->uiVersion != 2) {wcsncpy(lm->name, L"TestLink", 256);wcsncpy(lm->description, L"TestLink is a test of the Link plugin.", 2048);lm->uiVersion = 2;}lm->uiTick++;// Left handed coordinate system.// X positive towards "right".// Y positive towards "up".// Z positive towards "front".//// 1 unit = 1 meter// Unit vector pointing out of the avatar's eyes aka "At"-vector.lm->fAvatarFront[0] = 0.0f;lm->fAvatarFront[1] = 0.0f;lm->fAvatarFront[2] = 1.0f;// Unit vector pointing out of the top of the avatar's head aka "Up"-vector (here Top points straight up).lm->fAvatarTop[0] = 0.0f;lm->fAvatarTop[1] = 1.0f;lm->fAvatarTop[2] = 0.0f;// Position of the avatar (here standing slightly off the origin)lm->fAvatarPosition[0] = 0.001f;lm->fAvatarPosition[1] = 0.0f;lm->fAvatarPosition[2] = 0.5f;// Same as avatar but for the camera.lm->fCameraPosition[0] = 0.0f;lm->fCameraPosition[1] = 0.0f;lm->fCameraPosition[2] = 0.0f;lm->fCameraFront[0] = 0.0f;lm->fCameraFront[1] = 0.0f;lm->fCameraFront[2] = 1.0f;lm->fCameraTop[0] = 0.0f;lm->fCameraTop[1] = 1.0f;lm->fCameraTop[2] = 0.0f;// Identifier which uniquely identifies a certain player in a context (e.g. the ingame name).wcsncpy(lm->identity, L"Unique ID", 256);// Context should be equal for players which should be able to hear each other positional and// differ for those who shouldn't (e.g. it could contain the server+port and team)memcpy(lm->context, "ContextBlob\x00\x01\x02\x03\x04", 16);lm->context_len = 16;}February 13, 2017 at 8:18 pm #7142Sadly I get the same error using that code.
AmigaDOS12345678Build error: System command 'g++ -static -m32 -s -O3 -mwindows -o "G:/test/test.products/Windows/Test.exe" -Wl,@tmp/lnkFiles1.txt' failed.g++ -static -m32 -s -O3 -mwindows -o "G:/test/test.products/Windows/Test.exe" -Wl,@tmp/lnkFiles1.txtG:/test/test.buildv1.1.03/windows_release/build/_1src_2test_0test.cpp.o:test_test.cpp:(.bss+0xc): multiple definition of `lm'G:/test/test.buildv1.1.03/windows_release/build/_1include_2_0r.cpp.o:_r.cpp:(.bss+0x0): first defined herecollect2.exe: error: ld returned 1 exit statusAm I doing something wrong?
Monkey1234567891011121314151617#Import "<std>"#Import "<mojo>"Using std..Using mojo..#Import "mumble.h"Class MyWindow Extends WindowMethod New()EndEndFunction Main()New AppInstanceEndFebruary 13, 2017 at 9:04 pm #7143whenever a declaration create duplicates try to add static before it…
in this case I added 3 static keywords and no errors now (on W7)
this doesn’t mean it’s the good solution! but it avoids you spilltin to a .cpp src
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119[crayon-5cba8795db328859444185 inline="true" ]#ifdef _WIN32#include <windows.h>#else#include <sys/mman.h>#include <fcntl.h> /* For O_* constants */#endif // _WIN32struct LinkedMem {#ifdef _WIN32UINT32 uiVersion;DWORD uiTick;#elseuint32_t uiVersion;uint32_t uiTick;#endiffloat fAvatarPosition[3];float fAvatarFront[3];float fAvatarTop[3];wchar_t name[256];float fCameraPosition[3];float fCameraFront[3];float fCameraTop[3];wchar_t identity[256];#ifdef _WIN32UINT32 context_len;#elseuint32_t context_len;#endifunsigned char context[256];wchar_t description[2048];};static LinkedMem *lm = NULL;static void initMumble() {#ifdef _WIN32HANDLE hMapObject = OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, L"MumbleLink");if (hMapObject == NULL)return;lm = (LinkedMem *) MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(LinkedMem));if (lm == NULL) {CloseHandle(hMapObject);hMapObject = NULL;return;}#elsechar memname[256];snprintf(memname, 256, "/MumbleLink.%d", getuid());int shmfd = shm_open(memname, O_RDWR, S_IRUSR | S_IWUSR);if (shmfd < 0) {return;}lm = (LinkedMem *)(mmap(NULL, sizeof(struct LinkedMem), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd,0));if (lm == (void *)(-1)) {lm = NULL;return;}#endif}static void updateMumble() {if (! lm)return;if(lm->uiVersion != 2) {wcsncpy(lm->name, L"TestLink", 256);wcsncpy(lm->description, L"TestLink is a test of the Link plugin.", 2048);lm->uiVersion = 2;}lm->uiTick++;// Left handed coordinate system.// X positive towards "right".// Y positive towards "up".// Z positive towards "front".//// 1 unit = 1 meter// Unit vector pointing out of the avatar's eyes aka "At"-vector.lm->fAvatarFront[0] = 0.0f;lm->fAvatarFront[1] = 0.0f;lm->fAvatarFront[2] = 1.0f;// Unit vector pointing out of the top of the avatar's head aka "Up"-vector (here Top points straight up).lm->fAvatarTop[0] = 0.0f;lm->fAvatarTop[1] = 1.0f;lm->fAvatarTop[2] = 0.0f;// Position of the avatar (here standing slightly off the origin)lm->fAvatarPosition[0] = 0.001f;lm->fAvatarPosition[1] = 0.0f;lm->fAvatarPosition[2] = 0.5f;// Same as avatar but for the camera.lm->fCameraPosition[0] = 0.0f;lm->fCameraPosition[1] = 0.0f;lm->fCameraPosition[2] = 0.0f;lm->fCameraFront[0] = 0.0f;lm->fCameraFront[1] = 0.0f;lm->fCameraFront[2] = 1.0f;lm->fCameraTop[0] = 0.0f;lm->fCameraTop[1] = 1.0f;lm->fCameraTop[2] = 0.0f;// Identifier which uniquely identifies a certain player in a context (e.g. the ingame name).wcsncpy(lm->identity, L"Unique ID", 256);// Context should be equal for players which should be able to hear each other positional and// differ for those who shouldn't (e.g. it could contain the server+port and team)memcpy(lm->context, "ContextBlob\x00\x01\x02\x03\x04", 16);lm->context_len = 16;} - 
		AuthorPosts
 
You must be logged in to reply to this topic.