SDL2, FreeBSD and CodeBlocks - how to link libraries - sdl

I started using FreeBSD about 3 months ago. Before, I used to code on Windows with CodeBlocks and never had any problems. I was writing SDL2 games like Snake and Tetris. But I want to do the same on FreeBSD, which is somehow new for me, like every Linux system.
I want to link SDL2 libraries to the CodeBlocks on FreeBSD, but I can't manage to do this. I'm doing sth like in Windows:
In Compiler settings >> Search directories >> Compiler I set /usr/local/include/SDL2, which is a path to the SDL2 headers
In Compiler settings >> Search directories >> Linker I set /usr/local/lib, which seems to be the path to the SDL2 libraries, like "libSDL2..." etc.
Additionally I'm adding in Compiler settings >> Linker settings >> Other linker options phrases like "-lSDL2main -lSDL2 etc."
It seems to be the same options set like in Windows, but it can't find -lSDL2main, that's the kind of problem.
On the other hand I tried to link libraries this way Compiler settings >> Linker settings >> Link libraries and I chose all libraries like libSDL....so or with .a extension, but got "Linker problem error 1"
I'm trying to compile with clang, but I don't think so that difference between gcc and clang is a problem here. So... how to link it?
Give me some advices and explain why I'm wrong, please :)
Part of code (how I'm trying to include headers):
#include </usr/local/include/SDL2/SDL.h>
#include "header.h"
A. S.

Related

Eclipse for C++ not recognizing brewed GSL and Boost libraries

I am on a system running Mac OS X. I am using Eclipse Oxygen.2 for C/C++, in my case C++. I am attempting to create a project that uses the GSL and Boost libraries. However, Eclipse appears to not be able to locate the GSL library and appears to recognize the Boost library, but doesn't appear to be able to do anything included in the Boost library. Both libraries were installed using Homebrew. Here is the code I am using:
#include <iostream>
#include <gsl.h>
#include <boost/optional.hpp>
using namespace std;
int main () {
int x = 5;
gsl::owner<int*> p = &x;
boost::optional<string>;
return 0;
}
The errors are as follows:
With the line #include <gsl.h>, it shows fatal error: 'gsl.h' file not found.
With the line gsl::owner<int*> p = &x; it shows Symbol 'owner' could not be resolved.
With the line boost::optional<string>; it shows Type 'boost::optional<string>' could not be resolved. This one is weird because it appears to have no issues with the boost/optional.hpp file.
This other information would probably be helpful:
I am using the MacOS X toolchain. Should I switch to the Cross GCC toolchain?
Here are the library search paths listed under the project preferences > C/C++ Build > Settings > MacOS X C++ Linker > Libraries: /usr/local/Cellar/boost/1.66.0/include and /usr/local/Cellar/gsl/2.4/include.
I am following Trevor Payne's Let's Learn C++ tutorial series.
Looks like you've pointed the libraries, not the includes, at the header files.
Right click the protect in the Project Explorer. Select properties from the pop-up menu. Navigate in the Properties dialogue C/C++ General->Paths And Symbols. Select the Includes tab. Add /usr/local/Cellar/boost/1.66.0/include and /usr/local/Cellar/gsl/2.4/include to the C++ Language for all Configurations.
Remove the include directories from the Libraries tab and then head to the Library Paths tab to add the locations of the libraries, if you need any. Boost is often header only. GSL I don't know from Atom.
Great job identifying the right issues and providing good debugging information.
Since you have installed GSL and Boost with Homebrew, they are probably installed correctly and the compiler just can't find the correct header and library files -- And this is probably because you're using the incorrect paths in the wrong setting.
In the Linker > Libraries options, you're using the two include paths for Boost and GSL. These belong in a different path option for finding headers, not libraries (I'm not sure where it is exactly, I don't have a mac!). Make sure you're following the tutorial's instructions carefully here.
You do also need to put library paths in the Library options, however: e.g. /usr/local/Cellar/gsl/2.4/include should be something like /usr/local/Cellar/gsl/2.4/lib

Why can't I link this library using the GNU GCC Compiler on Windows (but on Linux I can)

I've been trying to link this CryptoPP library for some time now; no luck. Due to most of the resources relating to linking the CryptoPP library being on the Linux platform I temporarily installed Ubuntu on a VM and got CryptoPP working quite smoothly, however, as previously mentioned, it doesn't work on Windows.
I am using the CodeBlocks IDE on Windows 8 using the MinGW compiler.
I already have the static CryptoPP .a file built.
My 2 attempts at linking the CrypoPP library have been:
A:
- Add the locations of the header files to the compiler and linker search directories (so it doesn't give me those "Head not found" errors)
Add the library to the Link Libraries section (I added "libcryptopp")
I know that the compiler can see the Linked Library because when I change the name to the libcryptopp.a file to something else (libcryptopp1.a) it says "-llibcryptopp not found"
In the end I get a whole bunch of Undefined API error messages :(
B: I then tried to add the header and library files to my compiler directory in suspicion of my IDE's search directory somehow failing me, this was not the case; I received the same Undefined API error messages.
I am tired of fighting this, if anyone could please leave some tips or point something out that I'm doing wrong I would be very grateful.

gnu compiler from the command line

Im learning c++ and I compile from the command line. I have a problem when it comes to trying to add 3rd party libraries. I cant seam to figure out the linker system. Does anyone know a good tutorial or something like that?
For example I want to play around with the SDL2 library and ill use a command like this.
c++ -I/Library/Frameworks/SDL2.framework/Headers -L/Library/Frameworks/SDL2.framework/ -lSDL2 helloworld.cpp
and I get the error ld: library not found for -lSDL2
You need to put the linking flags last on the line:
g++ -I/Library/Frameworks/SDL2.framework/Headers helloworld.cpp -L/Library/Frameworks/SDL2.framework/ -lSDL2
I found out the answer. The following command compiled correctly. The include statement had to be changed to...
#include<SDL2/SDL.h>
and the correct compile command is...
c++ -o helloworld helloWorld.cpp -framework SDL2
I could also have used g++. On my system both c++ and g++ are symlinks to the same gnu compiler which happens to be the latest version I have installed on the system.
the option -L is a unix linker option and does not work on a MAC. The dev's for GCC were kind enough to include MAC specific linker options in the form of -framework. These serve to follow the mac tradition of how and where they like to store libraries. You can link several frameworks together by separating them with a comma. So for example i could also do -framework SDL2,SDL2_mixer as long as my source has
#include<SDL2_mixer/SDL_mixer.h>
When compiling this default search location for libraries is /Library/Frameworks. The include statement is cross platform compatable and the mac gnu linker knows that if I say
#include<SDL2/SDL.h>
that that header will be found at /Library/Frameworks/SDL2.framework/Headers
The -IPATH option still works on mac and can be used to pass alternate search locations for header and source files just like it works in unix.

Add library to existing project netbeans

I am adding extensions to an (another persons) existing project at my company. Now I want to import an existing library like boost to it. I am using netbeans for debugging the existing project. Now in order to import a library into netbeans usually 2 steps are followed:
Include directories
Linker-> Add Library.
However when I right click on my existing project the option of Linker->Add Library is not appearing. (Though I have included the directories as that option is there).
Can someone please guide me as to how should I add the library through linker to my existing project? My project is in C++
Assuming you are using unix/linux variants:
Directories for headers and library linking are two different things. Include directories will have the headers needed, but after compilation the actual compiled code that resides in the libraries (*.a, *.so, etc...) might also be required.
For example, if you are using pthreads, apart from the headers which you need to include, you also need libpthread.
When linking, you need to provide the flag for linking with pthread i.e: -lpthread
You can search using find or locate on a unix system to find the libraries. In my case, its in
/usr/lib/libpthread.so
Therefore,
gcc myfile.c -lpthread -o myfile
Will link myfile.c with pthread library
Whereas,
gcc -L/usr/local/lib/
Tells gcc to look under /usr/local/lib to search for the library (not the header!).
Telling netbeans where the headers are, isn't enough, it will probably give you linking errors. Telling netbeans where the libraries are, may be enough, as it might use the proper flags. If that also fails, then you have to specify both the library flags and the path.
Alternatively, you may use tools like cmake, make, etc which automate this process and provide a bit more control IMO.
See Link 1
See link 2

Compiler unable to find valid library path

I'm getting the following error:
ld.exe||cannot find -lD:\Libraries\boost_1_47_0\boost_1_47_0\stage\lib|
Even though the path is valid. Any thoughts on that?
Edit:
Thanks MichalR!
For others, in order to configure boost for gcc:
Download boost
install it according to instructions from boost site
In code::blocks in Settings/global variables add path to your boost library for base and lib fields (this should be located in stage/lib folder)
In code::blocks in Project/Build options..., linker settings tab, link libraries - add here those libraries
In code::blocks in Project/Build options...Search directories tab in compiler subtab add $(#boost) and in linker subtab add $(#boost.lib).
Done. I spent a few days on this, but it was worth it. Now I can use this great IDE with gcc compiler which at the moment of this writing is miles ahead of MS. I am not being stopped in my personal development by MS - C++11 here I come!
The params for ld are e.g.:
-larchive
-Lsearch_path
The first tells what library to look for, the latter specifies library search paths.
Perhaps you have these options mixed in your command line - it looks you have -l with a directory parameter.