Handmade Hero » Forums » Code » Compiling with Winelib on Linux
edsrzf
Evan Shaw
4 posts
#48 Compiling with Winelib on Linux
3 years, 11 months ago Edited by Evan Shaw on Dec. 2, 2014, 7:57 p.m.

Just thought I'd share that it's possible to compile and run on Linux with Winelib:

1
winegcc -mwindows code/win32_handmade.cpp


Up until day 10, this worked without any code modifications, but gcc doesn't support the __rdtsc intrinsic [EDIT: This is actually not true if you include x86intrin.h. See below.]. So I added this (which I stole from Stack Overflow) to the top of the file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#ifndef _MSC_VER
#ifdef __i386__
static __inline__ unsigned long long __rdtsc(void)
{
    unsigned long long int x;
    __asm__ __volatile__ (".byte 0x0f, 0x31" : "=A"(x));
    return x;
}
#endif
#ifdef __x86_64__
static __inline__ unsigned long long __rdtsc(void)
{
    unsigned hi, lo;
    __asm__ __volatile__ ("rdtsc" : "=a"(lo), "=d"(hi));
    return ( (unsigned long long)lo)|( ((unsigned long long)hi)<<32 );
}
#endif
#endif


This might also work on OS X, but I don't run OS X so I don't know.
cmuratori
Casey Muratori
813 posts / 1 project

Casey Muratori is a programmer at Molly Rocket on the game 1935 and is the host of the educational programming series Handmade Hero.

#65 Compiling with Winelib on Linux
3 years, 11 months ago

That doesn't sound like something you should have to do. I don't use GCC anymore, but on LLVM you should be able to use their RDTSC intrinsic directly. I would assume the same would be true on GCC, but I don't know.

From CLANG's intrin.h:
1
2
unsigned __int64 __rdtsc(void);
unsigned __int64 __rdtscp(unsigned int *);


No inline ASM necessary :)

- Casey
mmozeiko
Mārtiņš Možeiko
1826 posts / 1 project
#68 Compiling with Winelib on Linux
3 years, 11 months ago

Yes, __rdtsc() is available for gcc. You just need to do #include <x86intrin.h>
edsrzf
Evan Shaw
4 posts
#84 Compiling with Winelib on Linux
3 years, 11 months ago

Thanks! I searched around but didn't find anything mentioning that intrinsic. I added an edit to the original post.
maiqtheliar
M'aiq
3 posts
#254 Compiling with Winelib on Linux
3 years, 11 months ago Edited by M'aiq on Dec. 6, 2014, 11:16 p.m.

I tried adding x86intrin.h but I get this error

1
2
3
/usr/bin/ld: cannot find -lwinecrt0
collect2: error: ld returned 1 exit status
winegcc: gcc failed

Any idea on what am I missing? I tried looking in /lib/ and /lib/wine/ but the library is not there and google is not being any useful today :(

I'm using Arch if that helps

"M'aiq knows much, and tells some. M'aiq knows many things others do not."
edsrzf
Evan Shaw
4 posts
#256 Compiling with Winelib on Linux
3 years, 11 months ago

Have you installed wine?

1
pacman -S wine


I'm also on Arch fwiw.
maiqtheliar
M'aiq
3 posts
#257 Compiling with Winelib on Linux
3 years, 11 months ago

My bad, I had wine-git from the AUR, apparently it gets rid of some stuff when it's done compiling, thanks

"M'aiq knows much, and tells some. M'aiq knows many things others do not."
maiqtheliar
M'aiq
3 posts
#258 Compiling with Winelib on Linux
3 years, 11 months ago Edited by M'aiq on Dec. 6, 2014, 11:33 p.m.

[strike]My bad, I had wine-git from the AUR, it works now thanks[/strike]
Man this forum is confusing

"M'aiq knows much, and tells some. M'aiq knows many things others do not."
mmozeiko
Mārtiņš Možeiko
1826 posts / 1 project
#263 Compiling with Winelib on Linux
3 years, 11 months ago Edited by Mārtiņš Možeiko on Dec. 7, 2014, 8:25 a.m.

Not sure why you use winegcc to compile on Linux.

You should install mingw-w64-gcc package. MinGW is gcc based toolchain to create Windows executables/dll files. It provides whole toolchain (compiler, linker, resource compiler assembler, etc...) that allows you code to compile for 32-bit or 64-bit Windows. Use i686-w64-mingw32-g++ to compile 32-bit executable and x86_64-w64-mingw32-g++ to compile 64-bit executable.

Of course, to run windows executables on Linux you'll still need wine installed.
edsrzf
Evan Shaw
4 posts
#334 Compiling with Winelib on Linux
3 years, 11 months ago

Mingw would also work, but I used winegcc because it builds a native Linux binary and it's installed with Wine already.
mmozeiko
Mārtiņš Možeiko
1826 posts / 1 project
#337 Compiling with Winelib on Linux
3 years, 11 months ago

Sure, whatever works for you is good.
I just find it weird - to use Linux to build Windows code to Linux binary :) Imho you can simplify life by removing one step (Linux binary).
albatros
14 posts

None

#5740 Compiling with Winelib on Linux
2 years, 9 months ago

Do anybody had any luck making winegcc from the part when we load code dynamically ? (day 21 to 23)

I played with shared libraries and gcc options for quite some time now and I really have hard time with it.

None
mmozeiko
Mārtiņš Možeiko
1826 posts / 1 project
#5741 Compiling with Winelib on Linux
2 years, 9 months ago Edited by Mārtiņš Možeiko on Feb. 2, 2016, 11:10 p.m.

I haven't tried compiling whole Handmade Hero with winegcc, but simple dll/exe files work for me.

Here's dll.c file:
1
2
3
4
5
6
7
#include <stdio.h>

void
fun()
{
    printf("Hello from dll!\n");
}


Here's exe.c file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <windows.h>

int main()
{
    printf("Hello from exe!\n");

    typedef void fptr();

    HMODULE lib = LoadLibraryA("dll.dll.so");
    if (lib)
    {
        fptr* f = (fptr*)GetProcAddress(lib, "fun");
        if (f)
        {
            f();
        }
        else
        {
            printf("No fun found :(\n");
        }
    }
    else
    {
        printf("No library found\n");
    }
}


Here's dll.spec file (info on spec files):
1
@ cdecl fun()


Here's how I compile and run it:
1
2
3
4
5
$ winegcc -m32 -shared dll.c dll.spec -o dll
$ winegcc -m32 exe.c -o exe
$ ./exe.exe
Hello from exe!
Hello from dll!

I use -m32, because my wine environment is configured as 32-bit one, and my default gcc is 64-bit one. So I need to compile to 32-bit code.

But I'm still recommending to use mingw compiler and avoid linking to wine libraries. Compiled exe will run under the wine just fine.
albatros
14 posts

None

#5743 Compiling with Winelib on Linux
2 years, 9 months ago

Wow, Many thanks !

I will study this very soon :)

None
albatros
14 posts

None

#5757 Compiling with Winelib on Linux
2 years, 9 months ago Edited by albatros on Feb. 6, 2016, 10:22 p.m.

Yeah ! It works !

Just in case someone find it useful in the future, here is how to compile handmade hero day 25 on linux using winegcc:

Here is my build.sh
1
2
3
4
5
6
7
8
9
#!/bin/bash

mkdir -p ../build
pushd ../build

winegcc  -DHANDMADE_INTERNAL=1 -DHANDMADE_SLOW=1 -DHANDMADE_WIN32=1 -mwindows ../code/handmade.cpp ../code/handmade.spec -o handmadehero -g -Wno-write-strings -std=c++11 -shared -Wl, -fPIC

winegcc  -DHANDMADE_INTERNAL=1 -DHANDMADE_SLOW=1 -DHANDMADE_WIN32=1 -mwindows ../code/win32_handmade.cpp  -o handmadehero -g -Wno-write-strings -std=c++11
popd


Here is the content of handmade.spec:
1
2
@ cdecl GameUpdateAndRender()
@ cdecl GameGetSoundSamples()


I do a search & replace of handmade.dll to handmadehero.dll.so inside win32_handmade.cpp

And at last, I add theses two lines to win32_handmade.cpp
1
2
#include <x86intrin.h> //a ajouter pour compiler
#define _snprintf_s(...) snprintf(__VA_ARGS__)

(given that they start with a # which is a comment in bash, I keep them in my build.sh, ready to be cut and paste on each new handmade.cpp iteration)

None