gnustep / tools-windows-msvc

Objective-C on Windows using Clang and GNUstep.
MIT License
42 stars 10 forks source link

Issue with interface/implementation on Windows #6

Closed vivekg-ai closed 3 years ago

vivekg-ai commented 3 years ago

I build the toolchain as directed in this repositories instructions. After this, I was able to build the toolchain successfully.

Having said that, I am facing the below issue -

test.mm

#import <Foundation/Foundation.h>

@interface Employee : NSObject
    -(void) helloWorld;
@end

@implementation Employee
    -(void)helloWorld{
        NSLog(@"Hello World 2");
    }
@end

int main(int argc, const char * argv[]) {
    NSLog(@"Hello World 1");
    @autoreleasepool {
        Employee *employee = [Employee new];
        [employee helloWorld];
    }
    return 0;
}

Compilation environment

clang-cl -I C:\GNUstep\x64\Release\include -fobjc-runtime=gnustep-2.0 -Xclang -fexceptions -Xclang -fobjc-exceptions -fblocks -DGNUSTEP -DGNUSTEP_WITH_DLL -DGNUSTEP_RUNTIME=1 -D_NONFRAGILE_ABI=1 -D_NATIVE_OBJC_EXCEPTIONS -DGSWARN -DGSDIAGNOSE /MD /c test.mm

clang-cl test.obj C:\GNUstep\x64\Release\lib\gnustep-base.lib C:\GNUstep\x64\Release\lib\objc.lib C:\GNUstep\x64\Release\lib\dispatch.lib /MD -o test.exe

test.exe

and the output is as follows -

on Windows

2021-09-05 19:35:34.215 test[32408:17216] Hello World 1

However, on macOS I compiled it like this

clang++ test.mm -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.3.sdk -fobjc-arc

and the output is like -

2021-09-05 19:32:39.120 a.out[77611:5403627] Hello World 1
2021-09-05 19:32:39.120 a.out[77611:5403627] Hello World 2

So any pointers on what might be wrong here?

triplef commented 3 years ago

Looks like you didn’t pass -fobjc-arc on Windows. Does it make a difference when you add this? You’ll have to use -Xclang -fobjc-arc with clang-cl.

vivekg-ai commented 3 years ago

With the -fobjc-arc passing on windows also has the same result.

vivekg-ai commented 3 years ago

But got it working by using the msys2 and the below command -

. /c/GNUstep/x64/Debug/share/GNUstep/Makefiles/GNUstep.sh

clang `gnustep-config --objc-flags` -fobjc-arc test.mm `gnustep-config --base-libs` -ldispatch -o test.exe

🎉

triplef commented 3 years ago

Great to hear. I guess it comes down to some difference in the compiler or linker flags then. Would be interesting to know which.

If you'd rather use clang-cl you could dissect the output from gnustep-config --objc-flags and gnustep-config --base-libs to figure out what flags you are missing (some of them you'll have to prefix with -Xclang to pass them to clang-cl, but just try passing them without first and see if clang-cl complains).

Or you can copy the output from the above two commands and use that to call clang (instead of clang-cl) directly in a Windows shell, as MSYS2 is only needed to run gnustep-config.

triplef commented 3 years ago

Also does it work when you remove the @autoreleasepool?

vivekg-ai commented 3 years ago

Also does it work when you remove the @autoreleasepool?

Yes it works with arc. Great work! 🙏

triplef commented 3 years ago

Also does it work when you remove the @autoreleasepool?

Yes it works with arc. Great work! 🙏

Great! Do you mean it works when you add -Xclang -fobjc-arc, or without it when you remove @autoreleasepool (which should definitely be supported)?

triplef commented 3 years ago

I assume this is fixed on your end. Please re-open if not.

vivekg-ai commented 3 years ago

Great! Do you mean it works when you add -Xclang -fobjc-arc, or without it when you remove @autoreleasepool (which should definitely be supported)?

Yes I had to use -Xclang -fobjc-arc for ARC. And yes had to remove @autoreleasepool when I did not pass the flag.

The issue is resolved on my end. Thank you for looking into it! 👍