142 points · by vyuh · 9y ago · 96 comments
While this works, it's generally a bad idea to depend on LD_LIBRARY_PATH. This type of environment override should generally be left as a tool for sysadmins and other integration/workaround needs.
Instead, you can bake the library search path into the executable with ld's "-rpath" option.
gcc -o foo -Wall -Wl,-rpath,/lib/search/dir
> -sharedBuilding a shared library may also require building position independent object files. (CFLAGS += -fPIC). If the library is being distributed, it is also a good idea to set the soname[1].
This can get complicated very quickly, so it's probably better to let something like libtool[2] handle building and managing the library. (Autotools is a lot easier[2] to use that its reputation suggests. Way too many configure.ac scripts are a cargo cult mess)
[1] http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries...
From the libtool manual ([1]):
Currently it is not generally possible to install into a temporary
staging area that contains needed third-party libraries that are not
yet visible at their final location.
So to build against a shared lib provided by another package, that other package has to be installed at the final destination already.I seriously considered going back to just static libraries at some point. I believe the build system of git does away with autotools alltogether, consisting of just a simple Makefile.
Also, a link to phk's libtool rant seems apt here ([2]).
[1]: https://www.gnu.org/software/libtool/manual/html_node/Instal...
[2]: http://queue.acm.org/detail.cfm?id=2349257&ref=fullrss
Edit: typo; make links work
Second one works for me, but has already been discussed here back in 2012 eg. https://news.ycombinator.com/item?id=4407188 .
https://enchildfone.wordpress.com/2010/03/23/a-description-o...
This is pretty much the only sensible way to build an executable that statically relies on having a library shipped alongside it.
install_name_tool -change new_path old_link executable
and you can check paths with otool -L executableAnd -DPIC.
While also utterly horrible, I find CMake to be marginally less utterly horrible than Autotools, and most new projects seem to use CMake over Autotools.
A library is not rocket science, and knowing how they work is important to understand how your compiler works anyway.
Previous comments: https://news.ycombinator.com/item?id=12253252
The jlibtool I know (and that you seem to have implemented) however is a drop-in replacement for libtool to merely speed up libtool (rather than tar-and-feather libtool out of existence as it deserves to) isn't it? It seems to be used by apache httpd last I checked.
Anyway, cudos to those who do something about it. I tried a couple years ago, but haven't followed through.
I fixed a lot of bugs. It's now a 95% drop-in replacement for libtool. The builds are MUCH faster than with libtool.
Modern compilers have error messages that show you the whole chain of #includes, and on top is the filename and line number, whether it ends with ".c" or ".h" or anything else.
Exe size -- Sean usually includes a macro that lets you include the implementation only once (so you can use it as a pure header in multiple places, then as a definition just once).
Compile times -- if you use that macro, the implementation will be skipped by the preprocesser which is really fast.
Even if you build the whole code multiple times in your project, C compilers are also very very fast these days.
Many people follow Sean's style but use C++. That can be much slower to compile if you use fancy features. But if you avoid templates it's usually fine.
It's interesting that many big C++ projects are also header-only, but for totally the opposite reason -- they use templates everywhere so nothing can be compiled separately. That approach definitely does slow down compilation and increase your code size.
Wow. I am speechless. Going to dive in another day.
It's weird because it does feel like it violates the separation of interface and implementation. On the other hand, it's really efficient. It has the advantages of doing generics with the C preprocessor (efficiency and de-duplication of code, if nothing else), minus many of the disadvantages of doing generics with the C preprocessor.
Putting non-template code or data in headers is something I've seen people do for expediency, but it's more trouble than it's worth IMO. As soon as two files in the same project include it, you risk a future headache.
/Pedantic
(Templates get de-duplicated).
But if you need anything more than that it's all there. e.g. loading from memory, loading via callbacks, support for HDR images, support for custom allocators, preprocessor flags let you exclude code for unused image formats, etc etc
A better approach would be to use the $ORIGIN linker keyword (-R works in gcc by special dispensation for compatibility with every other linker in existence, and is portable, as opposed to -rpath):
gcc -L/home/newhall/lib -R'$ORIGIN:$ORIGIN/../lib' -lmine -o myprog myprog.c
because unfortunately the $ORIGIN keyword includes the $, to embed it effectively into Makefiles and shell scripts, the following is needed in the environment: O='$$O'; export O
ORIGIN='$ORIGIN'; export ORIGIN
this way, both shell and make will expand $ORIGIN to $ORIGIN, without the need to recursively escape anything. $ORIGIN linker keyword is supported by GNU ld, Solaris / illumos ld, and HP-UX's ld.https://blogs.oracle.com/ali/entry/avoiding_ld_library_path_...
https://blogs.oracle.com/rie/entry/tt_ld_library_path_tt
Also, unless one is an operating system vendor, one may not install anything into /usr, as that is the vendor's namespace and having a clash with the vendor could bring one's production down in case of upgrades, were the vendor to deliver the same software. The System V and LSB FHS specifications state that /opt is for 3rd party and unbundled applications, /etc/opt for configuration, and /var/opt for data.
Unfortunately, the essay says nothing about library interface versioning with linker map files, for backward compatibility, so that one single shared object library can be used by all versions of the consuming software simultaneously.
>.. does absolutely nothing to help build your code ..
I don't know much about cargo nor rust. How does it help build your code ? edit: nvm, i found how [0]
>.. manage dependency versions ..
All package managers do. Granted not implicitly when building your source, as you have to install them yourself. Good libraries can have multiple versions installed at the same time (i won't go into nix and such).
>.. control how dependencies are built ..
Most (of the used) programming languages don't need to build dependent libraries, they load them dynamically.
Note that I don't differentiate between code in headers, code in libraries, or even code in other programs that the specific program would communicate with via some IPC. (side note: i have grown fond of single header libraries, for their simplicity)
Also to quote the rust docs "Rust has two distinct terms that relate to the module system: ‘crate’ and ‘module’. A crate is synonymous with a ‘library’ or ‘package’ in other languages." Not 100% what i was saying, but it does hint on how similar cargo is to a "normal" package manager.
It seems that everyone wants to build in a package manager these days. Rust, racket, haskell, python... What is wrong with copy-ing code directly ? Do we need these ? It just reminds me of leftpad.
Huh? There is no copying at runtime when using a static library. Or have I misunderstood this all these years?
If your goal is to have a library that runs on most Unix-like and Windows systems, then '#pragma once' works just fine. But, if there's a possibility that your code would be deployed to an odd platform, it's probably best to use traditional header guards. I'm so used to using header guards that the appeal of '#pragma once' isn't that great for me.
2 - it offers no performance benefit on sensible compilers (which have a special case in them that can detect the typical header guard pattern and so avoid reading a header twice)
3- it is non standard, and the feature it provides is one which is easily implemented within the standard, where the only disadvantage seems to be that you need to type more characters.
Is there some performance gain from #pragma once ?
> Is there some performance gain from #pragma once?
No. Include guards can be detected and optimized just the same.
IIRC, it does only if I use the Refactor option inside the IDE.