Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call on a function found on another file?

People also ask

How do you call a function from another file in main C?

You must declare int add(int a, int b); (note to the semicolon) in a header file and include the file into both files. Including it into Main. c will tell compiler how the function should be called.

How do I import a function from another file?

To use the functions written in one file inside another file include the import line, from filename import function_name . Note that although the file name must contain a . py extension, . py is not used as part of the filename during import.

How do you call a function from another program in C++?

You need the address of the process in it's virtual address space. To do so, use EnumProcessModules and GetModuleFileNameEx to find the filename of the module with the function you want. (Could be an EXE or a DLL.)

How do I reference another cpp file?

You can create a file called player. h declare all functions that are need by other cpp files in that header file and include it when needed. #include "stdafx. h" #include <SFML/Graphics.


You can use header files.

Good practice.

You can create a file called player.h declare all functions that are need by other cpp files in that header file and include it when needed.

player.h

#ifndef PLAYER_H    // To make sure you don't declare the function more than once by including the header multiple times.
#define PLAYER_H

#include "stdafx.h"
#include <SFML/Graphics.hpp>

int playerSprite();

#endif

player.cpp

#include "player.h"  // player.h must be in the current directory. or use relative or absolute path to it. e.g #include "include/player.h"

int playerSprite(){
    sf::Texture Texture;
    if(!Texture.loadFromFile("player.png")){
        return 1;
    }
    sf::Sprite Sprite;
    Sprite.setTexture(Texture);
    return 0;
}

main.cpp

#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "player.h"            //Here. Again player.h must be in the current directory. or use relative or absolute path to it.

int main()
{
    // ...
    int p = playerSprite();  
    //...

Not such a good practice but works for small projects. declare your function in main.cpp

#include "stdafx.h"
#include <SFML/Graphics.hpp>
// #include "player.cpp"


int playerSprite();  // Here

int main()
{
    // ...   
    int p = playerSprite();  
    //...

Small addition to @user995502's answer on how to run the program.

g++ player.cpp main.cpp -o main.out && ./main.out