Thanks, this works:

{.emit:"""

#include <algorithm>

void nth_element(float* arr, int size, int n) {
    std::nth_element(arr, arr + n, arr + size);

} """.}

proc nth_element*(array: ptr float32, size: int, n: int) {.importcpp: 
"nth_element(@)".}

> var arr = newSeq[float32](https://forum.nim-lang.org/10)
> 
> for i in 0 ..< arr.len:
>     arr[i] = i.float32
> 
> nth_element(addr arr[0], arr.len, 5)

However if I try to export the nth_element function and import from another 
module, I have an error:

.cache/nim/test1_d/@mtest1.nim.cpp: In function ‘void NimMainModule()’:

.cache/nim/test1_d/@mtest1.nim.cpp:1249:25: error: ‘nth_element’ was not 
declared in this scope
    

1249 | nth_element((&arr__test49_u28.p->data[((NI)0)]), 
[T23](https://forum.nim-lang.org/postActivity.xml#T23), ((NI)5));
    | ^~~~~~~~~~~

Error: execution of an external compiler program 'g++ -c -std=gnu++17 
-funsigned-char -w -fmax-errors=3 -fpermissive -pthread 
-I/home/....../.choosenim/toolchains/nim-2.0.4/lib 
-I/home/....development/vex-core/tests -o 
/home/...../.cache/nim/test1_d/@mtest1.nim.cpp.o 
/home/...../.cache/nim/test1_d/@mtest1.nim.cpp' failed with exit code: 1

it seems it cannot find the cpp declaration from the importing module.. how can 
I make that declaration available? Or should I compile this module first and 
import it compiled?

Reply via email to