Skip to content

How to link with C static library ? #18557

Discussion options

You must be logged in to vote

@leonsal

I think I found a solution; at least I got the following small example working:

// test.c
int test(void) {
    return 42;
}
// test.h
extern int test(void);

Now, I compiled and bound test.c as follows:

gcc -c -std=c99 -Wall -Werror -Wextra -Wpedantic test.c
ar rs libtest.a test.o

To make sure that neither the C file nor its object code would be called from V, I removed both the test.c and test.o.

// test.v
#flag -ltest
#flag -L.
#flag -I .
#include "test.h"

fn C.test() int

fn main() {
        println('${C.test()}')
}

And finally, I compiled the test.v with v test.v which gave me the executable ./test; running ./test will print the expectation: 42.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@kevinmatthes
Comment options

@JalonSolov
Comment options

Answer selected by spytheman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants