# Can you use a library function inside another function?

This topic is 1607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

It's been a very long time since I've done anything in programming, but am about to get back but have forgotten a little bit about it.

I admit this is a No-duh question, but I just wanted to make sure

(Referring to C++)

As you could create your own functions for reuse and call later, will it work if you make functions with functions inside from other libraries or API (SDL)? And if theres anything that needs to be done to do that, and be able to reuse it, I'd like to know. Like maybe when or where do I have to include the original library, in the file with my functions, or in the main code of a project where I use my functions?

Example:

**Not an actual c++ code, just trying to show the idea

int mydraw (a,b,c,d,e) {

SDLdraw (a)

SDLX(b)

SDLY(c)

SDLbpp(d)

SDLtime(e)

}

My plan was to make a little SDL (C++) based Engine/library to shorten the process of developing some games I have in mind, to suit my own needs.

##### Share on other sites

The header file that declares mydraw' may not need to include any SDL headers. The .cpp file where you define mydraw' has to include the SDL headers needed to make those library function calls.

No offense, but this type of question belongs in the "For Beginners" forum.

##### Share on other sites

Yes you can call other library functions from within your own functions as long as the correct library headers are included (and you will get a compile time error if they are not as your compiler wont know what SDLtime() or any of those functions are)

So - if I were you, I would put all of my function declarations in a header file like engine.h

and then all of the function implementations in a cpp file like engine.cpp

in the top of your engine.cpp file you would then include engine.h and the needed SDL headers

in your main.cpp simply include your engine.h file and you should be good to go

Also - I don't know about SDL - but usually libraries must have their .lib files included in the project library files - how to do this depends on your IDE - you should be able to add the library's include directory path to your include directory path so that you can type "#include <SDLMain.h> rather than "#include <c:\Docs\BlaBlaBla\SDLMain.h>"

##### Share on other sites

If you're creating a custom DLL called 'myEngine.dll', then your DLL's source files need to #include SDL's header files to access the function names (the function "declarations") of SDL functions.

However, your custom DLL doesn't need to contain SDL's DLLs, but when your executable (myGame.exe) links to your DLL, it also needs to link to SDL's DLLs so the implementation (the function "definitions") of SDL's functions can be included into the resulting executable.

Edited by Servant of the Lord

##### Share on other sites

You will always be inside one of your functions when calling a library function.  At the very least you will be in main.  A minor exception being constructing globals.

Edited by EddieV223

##### Share on other sites

This topic is 1607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628710
• Total Posts
2984340

• 23
• 11
• 10
• 13
• 14