Help example cimport lib.a please

hello, how to do in build and src.zig
this is a good example to include lib.a in a zig program

my program in c++ works perfectly
with an int main etc…

//#include <iostream>
#include <unicode/unistr.h>
#include <string>

const std::string sToUpper(const std::string svar) {
    icu::UnicodeString us(svar.data());
    us.toUpper(); //convert to uppercase in-place
    std::string s;
    us.toUTF8String(s);

    //std::cout<<"Upper: "<<s<<"\n";
    return s;
}


const std::string  stoLower(const std::string svar) {
    icu::UnicodeString us(svar.data());
    us.toLower(); //convert to lowercase in-place
    std::string s;
    us.toUTF8String(s);
    return s;
}



const std = @import("std");

const fconv = @cImport({
    @cInclude("Transform.h");
});


pub fn main() !void {
var str: [*c] const u8 = "adréèà";
const str2 :[*c] const u8 = "";

std.debug.print(" text: {s}    {s}",.{str ,str2});

str2 = fconv.sToUpper(str);

}```


str2 = fconv.sToUpper(str);  error str


---------------------------





const std = @import("std");

pub fn build(b: *std.build.Builder) void {
    const target = b.standardTargetOptions(.{});
    const mode = b.standardReleaseOptions();
    const prog = b.addExecutable("test", "src-zig/test.zig");
    prog.setTarget(target);
    prog.addPackagePath("fconv", "src_c/Transform.h");
    const Transform = b.addObject("Transform", "obj/Transform.o");
    prog.setBuildMode(mode);

    prog.addIncludeDir("src_c/");
    prog.addLibPath("lib/");
    prog.linkSystemLibrary("stdc++");
    prog.linkSystemLibrary("icuuc");
    prog.linkSystemLibrary("X11");
    prog.addObject(Transform);

    prog.install();

    const run_cmd = prog.run();
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }

    const run_step = b.step("tested", "Run the app");
    run_step.dependOn(&run_cmd.step);
}

I’m not sure I understand what’s the question. Could you rephrase it?

I think they’re asking how to link with the lib.a file that contains those two C functions, and how to call them from Zig.

1 Like

What I see is C++ code, not C code.

The C++ code is exporting two C ABI functions that they want to call, but yes I should have said C++ code.

1 Like

I manage to make my lib, and the build with the object,

but then I stall

To use a c++ library you need a c wrapper around it, but in this case it is unnecessarily complicated because you are just using the icu library, which already can be used as a c library directly.

So yeah, just use the c interface to the icu library directly from zig, your c++ library isn’t needed.
https://unicode-org.github.io/icu-docs/apidoc/released/icu4c/


Here is an example of how the c library is used: icu/ucase.c at main · unicode-org/icu · GitHub (should make it easier to see how you would use it from zig)
It seems most of the other examples are in c++ only, for those you probably have to look at the c api reference or the header files directly.

The code as it was before the edits exported two functions returning std::string, which I’m reasonably sure wouldn’t be useful in a C (or Zig) context.

1 Like

I’m going on this new test from the iuc4c lib

icu::UnicodeString us(t);

how do you translate that

That’s a C++ type. They asked you to use the C interface (not the C++ interface) of the iuc library. Please follow the link they gave and look for the C interfaces.

1 Like

best to use ziglyph
I tried, but too complicated for the moment, the “C” interface does not offer me simplicity, but I would come back to it
Thank you

1 Like