Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers

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
add comment

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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.