Skip to content
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

compile pragma name collision #10299

Closed
richard-jansson opened this issue Jan 14, 2019 · 8 comments

Comments

Projects
None yet
4 participants
@richard-jansson
Copy link

commented Jan 14, 2019

When creating a nim binding the compile pragma should not refer to the same filename / basename as the c file.

Example

{.compile: "/home/richard/proj/nimlisten/test.c".}
{.passC: "-I/home/richard/proj/nimlisten ".}

proc init*(port: cint) {.cdecl, importc: "init".}

Given that the nim file has the same filename as the c file. There'll be a collision on the temporary object files. The test.c.o path is specified twice in the argument given to the C compiler.

Current Output

richard@richard-ThinkPad-T430:~/proj/nimlisten$ nim c -r nimlisten.nim
Hint: used config file '/home/richard/src/Nim/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: nimlisten [Processing]
Hint: test [Processing]
CC: nimlisten
Hint:  [Link]
/home/richard/.cache/nim/nimlisten_d/test.c.o: In function `init':
test.c:(.text+0x0): multiple definition of `init'
/home/richard/.cache/nim/nimlisten_d/test.c.o:test.c:(.text+0x0): first defined here
/home/richard/.cache/nim/nimlisten_d/test.c.o: In function `close':
test.c:(.text+0x24): multiple definition of `close'
/home/richard/.cache/nim/nimlisten_d/test.c.o:test.c:(.text+0x24): first defined here
/home/richard/.cache/nim/nimlisten_d/nimlisten.c.o: In function `PreMainInner':
nimlisten.c:(.text+0x25): undefined reference to `unknown_testDatInit000'
nimlisten.c:(.text+0x2f): undefined reference to `unknown_testInit000'
/usr/bin/x86_64-linux-gnu-ld: /home/richard/proj/nimlisten/nimlisten: hidden symbol `unknown_testDatInit000' isn't defined
/usr/bin/x86_64-linux-gnu-ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
Error: execution of an external program failed: 'gcc   -o /home/richard/proj/nimlisten/nimlisten  /home/richard/.cache/nim/nimlisten_d/test.c.o /home/richard/.cache/nim/nimlisten_d/nimlisten.c.o /home/richard/.cache/nim/nimlisten_d/stdlib_system.c.o /home/richard/.cache/nim/nimlisten_d/test.c.o    -ldl'

Highlight of problematic part note that test.c.o is included twice.

Error: execution of an external program failed: 'gcc   -o /home/richard/proj/nimlisten/nimlisten  /home/richard/.cache/nim/nimlisten_d/test.c.o /home/richard/.cache/nim/nimlisten_d/nimlisten.c.o /home/richard/.cache/nim/nimlisten_d/stdlib_system.c.o /home/richard/.cache/nim/nimlisten_d/test.c.o    

Expected Output

richard@richard-ThinkPad-T430:~/proj/nimlisten$ nim c -r nimlisten.nim
Hint: used config file '/home/richard/src/Nim/config/nim.cfg' [Conf]
Hint: system [Processing]
Hint: nimlisten [Processing]
Hint: testi [Processing]
CC: nimlisten
Hint:  [Link]
Hint: operation successful (12325 lines compiled; 0.212 sec total; 16.23MiB peakmem; Debug Build) [SuccessX]
Hint: /home/richard/proj/nimlisten/nimlisten  [Exec]
initialzing at 20
hello

Possible Solution

Changing the name of the file see additional information for example.

According to shasclick on irc it's possible to tell compile to generate unique o files.
https://github.com/genotrance/nimgen/blob/master/src/nimgen/gencore.nim

Additional Information

Here it doesn't work
richard-jansson/nimlisten@cc12c5c
Renaming does the trick:
richard-jansson/nimlisten@41e6ac4

richard@richard-ThinkPad-T430:~$ nim -v
Nim Compiler Version 0.19.9 [Linux: amd64]
Compiled at 2018-12-05
Copyright (c) 2006-2018 by Andreas Rumpf

git hash: 24106ade8fee790fcfbab17d6c7825d79e05c7f3
active boot switches: -d:release
@krux02

This comment has been minimized.

Copy link
Contributor

commented Jan 14, 2019

Well I guess the compiler could check for colliding names and provide a better error message than this.

@Araq

This comment has been minimized.

Copy link
Member

commented Jan 14, 2019

Nah, we can simply simulate a nim Nimble package that everything is in.

@genotrance

This comment has been minimized.

Copy link
Contributor

commented Jan 14, 2019

This also breaks when you have the same file name but in different directories like in some C/C++ libraries. Since nimcache is a flat directory, everything just gets the same name.

I've worked around this in nimgen by generating the correct compile pragma but a better message will definitely help to save debug time.

@krux02

This comment has been minimized.

Copy link
Contributor

commented Jan 14, 2019

I don't have anything specific against flat simple name conversions without any visual noise added to the process. For example my editor plugin relies on the fact that the nim file and the generated c file have the exact same name.

@richard-jansson

This comment has been minimized.

Copy link
Author

commented Jan 15, 2019

@genotrance

This comment has been minimized.

Copy link
Contributor

commented May 25, 2019

I don't think this will fix all cases - if you have abc/test.c and def/test.c, they will still collide. This commit only handles Nim and C file collision.

@genotrance

This comment has been minimized.

Copy link
Contributor

commented May 25, 2019

Here's how nimterop deals with C file collisions.

https://github.com/nimterop/nimterop/blob/master/nimterop/cimport.nim#L442

@genotrance

This comment has been minimized.

Copy link
Contributor

commented May 25, 2019

Never mind, it is being tracked separately in #9370.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.