Giter Site home page Giter Site logo

Comments (2)

therecipe avatar therecipe commented on August 28, 2024

It seems like the go build tool and therefore qtdeploy will ignore all .mm files (.m files work through)

So, you probably have to place your c++ functions inside some .cpp files and make them available to go/obj-c with extern "C", if you want to have it auto compiled with go build/qtdeploy

But, I'm not sure and there might be some way that I don't know.
(Maybe ask in golang-nuts, /r/golang ?)

from qt.

therecipe avatar therecipe commented on August 28, 2024

Okay, I got it working.

You need to compile the .mm file separately and create a static lib, so that you can link it against go.

Just run go generate && go build

main.go

package main

/*
#cgo LDFLAGS: -framework Foundation -L. -l main
#include "main.h"
*/
import "C"
import (
    "os"

    "github.com/therecipe/qt/core"
    "github.com/therecipe/qt/widgets"
)

//go:generate qtmoc
type GoObject struct {
    core.QObject

    _ func() `slot:sayHello`
}

//go:generate clang++ -pipe -O2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -mmacosx-version-min=10.7 -Wall -W -fPIC -DQT_NO_DEBUG -DQT_CORE_LIB -I/usr/local/Qt5.5.1/5.5/clang_64/mkspecs/macx-clang -F/usr/local/Qt5.5.1/5.5/clang_64/lib -I/usr/local/Qt5.5.1/5.5/clang_64/lib/QtCore.framework/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/OpenGL.framework/Headers -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/AGL.framework/Headers -c -o ./libmain.o ./main.mm
//go:generate ar crv ./libmain.a ./libmain.o

func main() {
    widgets.NewQApplication(len(os.Args), os.Args)

    var goObject = NewGoObject(nil)
    goObject.ConnectSayHello(func() { println("Hello from Go") })

    var button = widgets.NewQPushButton2("sayHello", nil)
    button.ConnectClicked(func(_ bool) { C.sayHelloWrapper(goObject.Pointer()) })

    var layout = widgets.NewQVBoxLayout()
    layout.AddWidget(button, 0, 0)

    var window = widgets.NewQMainWindow(nil, 0)
    window.Layout().DestroyQObject()
    window.SetLayout(layout)
    window.Show()

    widgets.QApplication_Exec()
}

main.h

#pragma once

#ifndef GO_OBJC_H
#define GO_OBJC_H

#ifdef __cplusplus
extern "C" {
#endif

void sayHelloWrapper(void* ptr);

#ifdef __cplusplus
}
#endif

#endif

main.mm

#include "main.h"
#include "moc.h"

#import <Foundation/Foundation.h>
#include <QDebug>

void sayHelloWrapper(void* ptr){
    NSLog(@"Hello from Objective-C");

    GoObject_SayHello(ptr);

    qDebug() << "Hello from C++";
}

from qt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.