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
"How to call C code from Sage" thematic tutorial #17605
Comments
Branch: public/17605 |
Commit: |
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:3
I don't know enough about this to say whether it is all correct, but it looks very helpful (at least, to me!). Did you stop early, though?
I guess you have compiled the library file, but where is the calling part? |
comment:4
I am an idiot. I made many attempts to link this .pyx file with a compiled library and failed, and this is just the beginning of the section that I do not know how to write. I will update my patch in a second to remove that. I would love to write it, but I just don't know how. There are quite some things that I know how to do by modifying sage, but not without that Nathann |
Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:
|
comment:6
Well, the point is actually that with this you can give it a try and witness that it does work Volker? Do you know how to link to a compiled library while outside of Sage? Can you teach me? And I'll write the doc! Nathann |
comment:8
Note: An object file (.o) is not a library (.a / .so). You can link compiled C/C++ code with a stand-alone Cython project like in https://github.com/vbraun/lattice_phi4. Then just import as module in Sage. |
comment:9
Hello Volker,
I tried it but I was not able to compile it. It seems that I am missing some headers. I kept on whipping my test files and go to something new, though: I was able to make it work provided that I copied libdoublevector.so to SAGE_ROOT/local/lib/. Now, I am trying to make it all run from my working directory but I meet the following problem:
As a result the second call always fails, because the library is not found.
The header of my .pyx file is:
Thanks to you if you can give me some hint. In the meantime I'll be trying again to make it work. Nathann |
Branch pushed to git repo; I updated commit sha1. New commits:
|
comment:11
Okay found a way Needs review again ! Nathann |
This comment has been minimized.
This comment has been minimized.
comment:13
There is probably some alternate reality where the new description is useful for others to read/search, I just lack the imagination for it. Once you install the shared library into Sage you might just as well go through |
This comment has been minimized.
This comment has been minimized.
comment:14
There is probably one in which comments like that don't make people uselessly angry. Are we in the reality in which you also review the branch ?
I do not understand this comment. Nathann |
comment:15
Replying to @nathanncohen:
Presumably you want to build a stand-alone C/C++ and Cython project without having to install stuff into Sage. By definition, this should exclude steps that modify Sage directly. I mentioned the code in my github repo, this is the only way to do this currently: Write Makefile that calls gcc and cython directly. Also, you shouldn't build shared libraries the way you describe. Its very platform dependent and your steps won't work under OSX, say. |
comment:16
I do not understand. In the tutorial no instruction advises the user to change anything inside of Sage. What did you see that I missed?
I want to make it as simple as possible, and the current instructions avoid that.
Oh. Could you tell me how to build this library under OSX ? I will update the tutorial to explain both situations. Nathann |
comment:17
Copying files to SAGE_ROOT/local/lib/ is modifying Sage in my book. E.g. on a global installation (like SMC) you just don't have permissions to do that. |
comment:18
The tutorial does not do that. Nathann |
comment:19
Sadly, not today :( though it would be more fun than what I have to do. Though I would recommend removing the
I think Volker is referring to the earlier comment
which however I think you are right is not in the tutorial, you seem to have taken a different approach. As for the OS X, I think the |
comment:20
Hello,
Okay I can do that... I saw Jeroen's home path in several documents of the developer's manual, so I thought it did not matter.
Yes, probably.
HMmm.. Yep, but I need help to write that. I never used those architectures. Nathann |
comment:21
Probably not that much, but might as well make it more generic if you have the opportunity. |
comment:22
http://www.sagemath.org/doc/developer/doctesting.html I'll fix them all... Nathann |
comment:25
Replying to @kcrisman:
I changed those introduced by this branch.
For this kind of stuff, let's say that I prefer to make the changes and get my ticket reviewed.
I hope that just changing the prompt will make it (more) obvious in this case. Nathann |
comment:26
Yes, this does a good job. |
Reviewer: Karl-Dieter Crisman |
comment:27
Issues I found.
Maybe my file needs to be in my
SO tells me there are two errors - need to include stdio.h and then compile with
|
comment:28
Yo ! Thank you for looking at this.
My compiler does not say a word. I changed the return type of main to int and added a 'return 0'. I would prefer to remove this
Fixed.
I fixed the indentation level.
This I do not understand. Is hello_sage is in the same folder as hello.c ?
No no, you should not have to. And I would prefer to avoid asking the user to change the PATH anyway
Nono, you do not need to compile it by itself (and when you do it complains because no 'main' function is defined, which is correct). It gets compiled when you call "runfile". Actually, you do not need to compile 'hello.c' at first. It is just to give an example. If it gets complicated we can remove this compilation of "hello world", actually. It is not necessary.
When you say "similar problem", is it also about 'main' ? Nathann |
Branch pushed to git repo; I updated commit sha1. New commits:
|
Branch pushed to git repo; I updated commit sha1. New commits:
|
Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:
|
Branch pushed to git repo; I updated commit sha1. New commits:
|
Changed reviewer from Karl-Dieter Crisman to Karl-Dieter Crisman, David Coudert |
comment:35
Hello, I tried with my MBA/OSX Yosemite. Unless others have difficulties on other systems, I vote for positive review. David. |
comment:36
Yo,
Oh, so it works on Mac too ? Cool !
Well I'd vote for that.. And if there is something wrong with some distribution, we can add a note later. And hopefully in the meantime the guy will figure out how to make it work. Nathann |
comment:37
So let's go ;) |
comment:38
This also works for me now, though I have no idea why. Maybe because we removed the original compilation and Sage compiled it wherever Sage compiles things. |
comment:39
Ahahah. Well, remember? "Theory is when you know everything but nothing works, practice is when everything works but nobody knows why" Nathann |
Changed branch from public/17605 to |
This branch adds to Sage's thematic tutorial an explanation of how to interface a C code (or a compiled library) with Sage. It does not replace the developer's manual section entitled "Packing third-party code" as it is only a way to make it work "on your own compputer", without the complications that are only cause by Sage's infrastructure.
I have been asked the same instructions several times, and with this we will have a link to provide instead of the same explanation and the example files.
Nathann
CC: @sagetrac-azi @jm58660 @vbraun @kcrisman @anneschilling
Component: documentation
Author: Nathann Cohen
Branch/Commit:
2f37b6f
Reviewer: Karl-Dieter Crisman, David Coudert
Issue created by migration from https://trac.sagemath.org/ticket/17605
The text was updated successfully, but these errors were encountered: