Skip to content

cmd/cgo: cannot call functions with variable-argument lists. #975

Open

Description

@gopherbot

by akidan:

A package such as the following will not compile with cgo. 
(printf is a contrived example, but this would be useful for bridging objective c
through id objc_msgSend(id self, SEL op, ...);)

--- (file foo.go)
package foo

// #include <stdio.h>
import "C"

func PrintStuff() {
    C.printf(C.CString("Hello"))
}

---

$ cgo foo.go

What is the expected output?
Program compiles and is able to call function with variable-argument list.

What do you see instead?
unexpected type: ...

What is your $GOOS?  $GOARCH?
darwin amd64

Which revision are you using?  (hg identify)
43fd9bb529f0+ tip

Activity

  1. rsc commented on Jul 29, 2010

    @rsc
    Contributor

    Comment 1:

    I'm not sure this will ever work.  It's very hard to type check and do correctly.

    Labels changed: added priority-low, removed priority-medium.

    Owner changed to r...@golang.org.

    Status changed to LongTerm.

  2. gopherbot commented on Jul 30, 2010

    @gopherbot
    ContributorAuthor

    Comment 2 by akidan:

    Thank you for the update all the same - transparency is appreciated!
  3. rsc commented on Dec 9, 2011

    @rsc
    Contributor

    Comment 3:

    Labels changed: added priority-someday.

  4. rsc commented on Dec 4, 2013

    @rsc
    Contributor

    Comment 5:

    Labels changed: added repo-main.

  5. rsc commented on Mar 3, 2014

    @rsc
    Contributor

    Comment 6:

    Adding Release=None to all Priority=Someday bugs.

    Labels changed: added release-none.

  6. added this to the Unplanned milestone on Apr 10, 2015
  7. LionNatsu commented on Jan 24, 2018

    @LionNatsu
    Contributor

    I think this should be documented as it is a long-term issue.

    While wiki says:

    ... That is, if you want to call the C function printf() from Go code, you write C.printf(). Since variable argument methods like printf aren't supported yet (issue 975), we will wrap it in the C method "myprint":...

    The documentation of cgo should be more explicit for the issue, but now it not even mention this.

  8. 2 remaining items

  9. deleted a comment from on Nov 1, 2020
  10. deleted a comment from on Nov 1, 2020
  11. deleted a comment from on Nov 1, 2020
  12. deleted a comment from on Nov 1, 2020
  13. deleted a comment from on Nov 1, 2020
  14. bradfitz commented on Feb 2, 2021

    @bradfitz
    Contributor
  15. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Feb 2, 2021
  16. ianlancetaylor commented on Feb 7, 2021

    @ianlancetaylor
    Contributor

    n order to implement this, we need to be able to rewrite

    C.printf(C.CString("%d %s"), 1, C.CString("2"))
    

    into a call to a generated Go function

    GoWrapper(C.CString("%d %s"), 1, C.CString("2"))
    

    which means that we need to generate

    func GoWrapper(p1 *C.char,  p2 int, p3 *C.char)
    

    and similarly for a C wrapper function.

    That is all fine, but in order to write these functions we need to know the types of the Go arguments. Normally we can simply use the types of the C function parameters, which we can get from the C debug info. But of course for a C variadic function we have no type information for the variadic arguments. That means that we need to use the types of the Go arguments. But currently cgo does not know those types.

    So I think this has to wait for #16623.

  17. removed their assignment
    on Jun 22, 2022
  18. added
    cgoIssues affecting runtime/cgo and cmd/go
    on Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.cgoIssues affecting runtime/cgo and cmd/go

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址