Compile/Link on Unix question

Posted on 2013-11-18
Last Modified: 2013-11-19
Hi, I wonder if you can help me with a development issue related to "libcurl".  I am an experienced Windows developer but I need to do something on a BSD Unix system, and I am basically a novice with that.  I am trying to write a C program that links with this
open source library for doing http requests.  I have uploaded the libcurl  tar file, uncompressed it, and I believe I successfully built the library.  I have the directory /home/comaxis/curl/lib in which are contained the files libcurl.a,,, and From the directory /usr/home/comaxis, I use the command:
 cc demo.c -Lcurl/lib -lcurl, and receive no errors.  But when I try to run a.out, I get the
error "Shared object not found, required by a.out".

Now, following directions from the libcurl web site, I do instead:
cc demo.c /Lusr/local/lib -lcurl.  The executable produced by that works fine.

The problem is that the libcurl version that is in /usr/local/lib is older, and I would prefer to use the version that I just made.  What is the problem?

Another question, I don't really understand what I am doing with the -l switch.  There is nothing in the directory that I specify named "curl" - so how does the link even work?
Question by:jmckay321
  • 4
  • 3
  • 2
LVL 35

Assisted Solution

mccarl earned 250 total points
ID: 39658419
I believe I successfully built the library
It seems as though this is true, but the "building" step is all you have done. You probably should "install" it also, by doing a "make install" from the libcurl directory. Check the output of that to see exactly where it installs it (should be /usr/local/lib be check to make sure). Then you should be able to use the second version of the "cc" command that you posted, and it should use this updated libcurl library.

I don't really understand what I am doing with the -l switch
The -L and the -l switches work together to specify the library to use. -L specifies a path to search for library files, and -l (with a "lib" prefix and a ".so" suffix) specifies the library that you want. So in your example....

(Where the bold items below indicates the values entered for -L and -l)

/usr/local/lib/    +      lib    +      curl    +     .so



Author Comment

ID: 39658469
Yes I did do a make install, as in:

 ./configure --prefix=/usr/home/comaxis/curl
 make install

That is what created the directory /curl/lib and populated it with the .so file.

By the way, this is on a system owned by my ISP, so I don't have root and can't do anything to /usr/local/lib.
LVL 35

Expert Comment

ID: 39658733
this is on a system owned by my ISP
Ah, there's the missing link. So you could try and get the dynamic library loader to load your shared library (.so file) but the easiest way might be to just link your demo against the static version of libcurl (.a file). Try this line to compile the demo program...
cc demo.c -Lcurl/lib -Wl,-static -lcurl

Open in new window

It forces it to use the static library in that directory rather than the shared library.
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

LVL 86

Expert Comment

ID: 39659524
>>I have the directory /home/comaxis/curl/lib in which are contained the files libcurl.a,

If the static lib suggestion by mccarl does not work (which I doubt) you should try to download libcurl as a source package, unzip it and then do a './configure' and then 'sudo make install'. That will place the shared binary in the correct location and create the symlink ''. As a side note, another possibility would be to create the link manually and set '_LD_LIBRARY_PATH' to point to the directory it resides in.

BTW, your build does work because - in an analogy to Windows - you have the import library, but the system can't find the DLL at runtime.

Author Comment

ID: 39659558
The command: cc demo.c -Lcurl/lib -Wl,-static -lcurl

results in:

/usr/bin/ld: cannot find -lgcc_s

Sorry, I know I should be able to work it out myself at this point, and I will keep at it, but if there is some obvious solution to this let me know.
LVL 86

Expert Comment

ID: 39659568
No static version of the CRT installed? Hm, that's bad...

Can you try to rebuild/install libcurl then?

Author Comment

ID: 39659609
Yes I can recompile and install - that is what I did originally.  Not sure what "sudo" is but I did do a "make install" which resulted in it creating the curl/lib directory for me, where it put the files libcurl.a,,, and also the link: ->
LVL 86

Accepted Solution

jkr earned 250 total points
ID: 39659630
Then it does not place the files under '/usr/lib'? See what you '_LD_LIBRARY_PATH' is made up of, and if the directory where resides is not among the ones listed there, add it using

export _LD_LIBRARY_PATH=$_LD_LIBRARY_PATH:[path_to_libcurl]

Author Closing Comment

ID: 39660034
Thanks for your help.  I think I'm going to give up on this for now, as I'm chasing a problem that doesn't exist - I can use the already installed libcurl and it works fine.  Hopefully I won't need a feature in a newer version.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
FreeBSD on EC2 FreeBSD ( is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question