Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example, If my c++ code included math.h so that I can use sin().

When I compile my code, I need to add the linker, that is :

g++ main.cpp -lm

So my question is: Is there any way in which I don't need to add the -lm?

share|improve this question
No, you must specify the libraries to link to. – Dan D. Nov 19 '12 at 8:41

Firstly, the -lm isn't the "linker" it's the name of a library you are asking to link against.

Secondly, the simple answer to your question is no, there isn't a way to avoid needing to specify that - if you use routines from a library other than the main C library then you need to tell the compiler that so it knows where to find the routines.

share|improve this answer
Well, you could invoke the compiler through a tool that specifies the libraries you need to link to for you. You'd need to configure that tool to pass -lm to the compiler, but you'd only need to do that once. – David Schwartz Nov 19 '12 at 9:40

You could consider using make and Makefiles. Of course you still need the links to libraries, but you don't need to think about it anymore when compiling. This comes in handy especially when you are linking multiple libraries.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .