2

I've tried to essentially build the PhysFS library with Cmake, what I used was Code::Blocks mingw makefiles. I can import in my project, I can technically declare the PhysFS_init() function (although that gives me too few arguments error) yet when I add an argument it tells me PHYSFS_init is an undefined reference.

That sounds so silly to me, how can you recognize it when it has no argument, but pretend it doesn't exist when I add an argument? I've run CMake twice, but I'm simply getting this:

[...]\src\main.cpp|20|undefined reference to `PHYSFS_init'|

The code, if necessary (just a bit, the rest is irrelevant):

#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include "Globals.h"
#include "GameStates.h"
#include "GameWindow.h"
#include "GameMenu.h"
#include <physfs.h>

int main(int argc, char** argv)
{
///<|===================| Classes & Declarations |===================|>
    sf::Event ev;
    sf::RenderWindow winMain ( sf::VideoMode( WindowWidth, WindowHeight, 32 ), WindowTitle );
    sf::Image Icon;
    GameStates gameState;
    GameMenu TitleMenu;
    MenuButton buttons[4];
    PHYSFS_init(NULL);

Why is this happening? What do I have to do to be able to use PhysFS in my project? I've built the library with CMake, I included the library I need, and I tried to initialize PhysFS which gave me an error which makes me feel like the way C++ compiles program is plain silly.

Bugster
  • 1,552
  • 8
  • 34
  • 56

1 Answers1

9

This is a general explanation of how C++ libraries work:

PHYSFS probably consists of header files and a static/dynamic library. The header file is where all the functions/classes of the library are declared, but not defined. Thus, when you include the header file into your project, your compiler can now know that the functions you call in the library exist, and exactly what type of parameters they take etc.

So to use a library, you must include its header files, which means you must add its include directory to the project/cmakelists.txt.

#some similar path to this
include_directories( /path/to/PHYSFS/include)

Now, when you actually link the executable into a binary, these functions must be "resolved" so that the linker knows where they actually are, so that they can be called. This is called "linking" with the library.

So to use a library you must link its library files.

In cmake this is done as follows:

add_executable(myexe myexe.cpp)

#Add library directory paths here
link_directories( /path/to/PHYSFSlibs/)

#libPHYS.a or libPHYS.so if using gcc convention, libPHYS.lib if MSVC convention
#cmake allows us to use the name of the library, instead of the filename to be portable.
target_link_libraries(myexe PHYSFS) 

I am not particularly familiar with PHYSFS, but if you compiled it as a library, you should have one of these files to link to.

undefined reference to `PHYSFS_init

This is a problem with "linking" and usually means you failed to link the library, or the library is missing the implementation of this function. Linking the library should fix this.

EDIT

I misread your question, and assumed you were using cmake with your project and linking to PHYSFS.

First you must separately build PHYSFS, which will produce a library file (.a,.so or .lib) depending on the platform/IDE, then you must link that library file in your C::B linker options.

To do this:

Go to Project->Build options. Click Linker Settings.

Image (from here): Linker Settings in C::B

Add the library.

Realz Slaw
  • 3,138
  • 1
  • 24
  • 38
  • The physFS package I downloaded from the site has no ,lib files or .a files or anything like that, just headers, docs and source files. And in my project there is no "cmakelist.txt". Also, how do I add all those things to CMake? – Bugster Sep 28 '12 at 18:25
  • I misread your question, and assumed you were using cmake with your project and linking to PHYSFS. Edited at bottom. – Realz Slaw Sep 28 '12 at 18:31
  • It's the first time I'm using cmake so I don't know how to build physfs, any links that you can provide or a very fast how-to in a nutshell? – Bugster Sep 28 '12 at 18:33
  • You can either generate C::B project, or if you are on linux, or using msys, its very very simple: go to the physfs dir, make a new directory called build, and go to that directory. Next, type "`cmake ..`", then type "`make`". (hint: you can also use `cmake-gui ..`, which will allow you to see all the options the physfs project has). This should produce the library files somewhere in the build directory. – Realz Slaw Sep 28 '12 at 18:37
  • Awesome, thanks a lot for this, I have been struggling for such a long time with this, I never thought that the hardest part of making a game would be this. Thanks a ton. – Bugster Sep 28 '12 at 18:37