-
Notifications
You must be signed in to change notification settings - Fork 1
/
helloWorld.nim
28 lines (20 loc) · 925 Bytes
/
helloWorld.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import std/[compilesettings, strformat, strutils]
when defined(linux):
{.passL: "-Xlinker -rpath .".}
proc currentSourceDir(): string {.compileTime.} =
## Get the Source Directory at compile time
result = currentSourcePath().replace("\\", "/")
result = result[0 ..< result.rfind("/")]
# Get nimcache directory at compile time
const nimcache = querySetting(SingleValueSetting.nimcacheDir)
# Compile cpp to .o files at compile time
const helloWorldConstant {.used.} = staticExec(fmt"g++ -c {currentSourceDir()}/helloWorld.cpp -o {nimcache}/helloWorld.cpp.o")
# Pass the .o file to the linker
{.passL: fmt"{nimcache}/helloWorld.cpp.o".}
# Pass some flags to eliminate cpp runtime dependencies
{.passL: "-static-libgcc -static-libstdc++".}
proc helloWorld*(): cint {.importc: "HelloWorld", nodecl, discardable.}
## Prints "Hello World! from cpp"
when isMainModule:
echo "Hello World! from c"
helloWorld()