-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
linenoise compilation with cpp #3720
Comments
I thought (2) is implicitly done by the C compiler we invoke but I guess we call 'g++' for all of these but it works with clang? |
On my system it works with neither |
Still doesn't compile for me with import rdstdin
discard readLineFromStdin("> ")
This is because these functions use a char *linenoise(const char *prompt);
int linenoiseHistoryAdd(const char *line); Again not sure how best to fix this. |
Make the prototypes agree? No idea what the problem is, works for me with clang. |
How do I express |
option 1:lib/system/io.nim: option 2:or option 3:(pending #12150, separate header would not be needed) // t11579b.h
#include <stdio.h>
void fn(const char* a, char b) {
printf("%s %c\n", a, b);
} type cstring_const* {.importcpp:"const char*".} = cstring
# pending https://github.com/nim-lang/Nim/issues/12150, {.emit.} would be sufficient
proc fn(a: cstring_const, b: char) {.importcpp: "$1(@)", header: "t11579b.h".} = discard
proc main=
var a = "abc".cstring
fn(cast[cstring_const](a), 'b')
main() @xflywind can you please help me document this in https://nim-lang.github.io/Nim/manual.html#implementation-specific-pragmas-importcpp-pragma ? |
@timotheecour |
@timotheecour type cstring_const* {.importcpp:"const char*".} = cstring
# pending https://github.com/nim-lang/Nim/issues/12150, {.emit.} would be sufficient
proc fn(a: cstring_const, b: char) {.importcpp: "$1(@)", header: "t11579b.h".} = discard
proc main=
var a = "abc".cstring
fn(a, 'b')
main() distinct cstring doesn't generate type cstring_const* {.importcpp:"const char*".} = distinct cstring
# pending https://github.com/nim-lang/Nim/issues/12150, {.emit.} would be sufficient
proc fn(a: cstring_const, b: char) {.importcpp: "$1(@)", header: "t11579b.h".} = discard
proc main=
var a = cstring_const("abc".cstring)
fn(a, 'b')
main() |
I found a solution; adapted from your distinct idea: // t11579b.h
#include <stdio.h>
void fn(const char* a, char b) {
printf("v1 %s %c\n", a, b);
}
void fn(char* a, char b) {
printf("v2 %s %c\n", a, b);
} # main.nim
when true:
type cstringConstImpl {.importcpp:"const char*".} = cstring
type cstringConst = distinct cstringConstImpl
proc fn(a: cstringConst, b: char) {.importcpp: "$1(@)", header: "t11579b.h".} = discard
var a = "abc".cstring
fn(cast[cstringConst](a), 'b')
doAssert not compiles(fn(a, 'b'))
proc fn(a: cstring, b: char) {.importcpp: "$1(@)", header: "t11579b.h".} = discard
fn(a, 'b') prints: notemaybe we could support in future the shortcut: |
Final example {.emit: """
#include <stdio.h>
int fn(const char* a, char b) {
return 1;
}
int fn(char* a, char b) {
return 2;
}
"""
.}
type
cstringConstImpl {.importcpp:"const char*".} = cstring
constChar* = distinct cstringConstImpl
proc fn(a: constChar, b: char): int {.importcpp: "$1(@)".} = discard
proc fn(a: cstring, b: char): int {.importcpp: "$1(@)".} = discard
var a = constChar("abs")
doAssert fn(a, 'b') == 1
var b = cstring("abc")
doAssert fn(b, 'b') == 2 Wrapper {.emit: """
int fn(const char* a, char b) {
return 1;
}
"""
.}
type
cstringConstImpl {.importc:"const char*".} = cstring
constChar* = distinct cstringConstImpl
proc fn(a: constChar, b: char): int {.importc: "fn", nodecl.}
var x = constChar("abc")
doAssert fn(x, 'c') == 1
doAssert not compiles(fn("abc", 'b'))
doAssert not compiles(fn("abc".cstring, 'b')) Generates: /* section: NIM_merge_VARS */
N_LIB_PRIVATE const char* x__4x9bQioQdm79c9a2t8uhnty3A = "abc"; |
good; a few notes:
# t11600b.h:
template <typename T>
using myOwnMap = const T;
# main.nim:
when true:
type
CppConstImpl[T] {.importcpp: "myOwnMap", header: "t11600b.h".} = object
CppConst*[T] = distinct CppConstImpl[T]
ConstCharPtr* = ptr CppConst[char]
{.emit: """
int fn(const char* a, char b) {
return 1;
}
""".}
proc fn(a: ConstCharPtr, b: char): cint {.importc: "fn", nodecl.}
# var x = ConstCharPtr("abc")
var x = cast[ConstCharPtr]("abc")
doAssert fn(x, 'c') == 1
doAssert not compiles(fn("abc", 'b'))
doAssert not compiles(fn("abc".cstring, 'b')) maybe this should be in some
|
I dnnuo, but I will write a simple article to introduce this. https://dev.to/xflywind/wrap-const-char-in-the-nim-language-53no |
Currently
rdstdin
can't be compiled with the cpp target because it uses linenoise, which is a C library that fails when compiled as C++.I see two choices:
.c
file endings, similar to what's done already with.asm
, and then invoke the C compiler instead of C++I guess 2 is the better choice.
The text was updated successfully, but these errors were encountered: