Xcode 3 ctime declaration issues - c++

I am in the process of porting my c++ engine to mac, and so I used premake to generate an xcode project, which it does fine.
Box2D is built into the engine and one of its files "b2Broadphase.h" is including algorithm from the c++ standard library.
This is giving these errors: ::clock_t has not been declared and so on for all the using commands in the ctime file.
I cannot figure this out because when using premake to build a make file it runs fine and build a perfect library on OSX. Its only Xcode giving these errors.

My guess is that Xcode has not been configured to include the implementation files (.m or .cpp) or it has not been configured to link against a library which you are using. In general, you need to do two things: 1. include the headers 2. link against the libraries with the actual executable objects.
In Xcode, you do this by selecting the project (top-most item) in the file-browser panel on the left, and there is a section in the main area to choose which libraries to link to. You must specifically tell it to link against whichever libs you are using, even if you have imported their headers.
For .cpp or .m implementation files, you need to tell it to include that file in the target for compilation. This can be done either in the build settings (similar place to the lib inclusion) or else when you have a file selected, the inspector panel on the right has a little area for you to choose which targets to include the file in. (you only need to "include" implementation files like this, not .h files)

Related

How to use an external library for C++ in VS2017

I've been programming in Python for over a year now but am just learning C++ and am unfamiliar with how to go about using external libraries, CMake and github for that matter. I'm trying to use an external library called cpr - https://github.com/whoshuu/cpr. So far I've followed the instructions in the 'Usage' section of that link up to, but not including, the "add_subdirectory(cpr)" bit.
So far I've got the source code for cpr in the Visual Studio project folder of my C++ project. In the project properties I've then added into Include Directories (under VC++ Directories) "$(SolutionDir)site_libs\cpr\include" and I've added the same thing into Additional Include Directories (under C/C++ -> All Options). This means that the following code compiles just fine:
#include <cpr/cpr.h>
int main(int argc, char** argv) {
auto r =
cpr::Get(cpr::Url{"https://api.github.com/repos/whoshuu/cpr/contributors"},
cpr::Authentication{"user", "pass"},
cpr::Parameters{{"anon", "true"}, {"key", "value"}});
r.status_code; // 200
r.header["content-type"]; // application/json; charset=utf-8
r.text; // JSON text string
}
However this code isn't working when it comes to building, due to link errors. I'm pretty sure the thing I'm missing is the actual .lib file for it to find where these functions etc are defined (nothing for cpr is set in the Linker -> Input property). So I'm wondering - how do I create this .lib file / is that even the right thing to do / what is this "add_subdirectory(cpr)" and where/how do I run it... basically what do I do to make this whole thing work..?! I've tried compiling cpr with CMake but it throws a load of errors about 'CMakeLists.txt' not present in certain folders.
Apologies if I've used any incorrect terminology here, only been learning C++ for a couple of days now. Any help massively appreciated!
If the library does not come as a binary distribution (that is with the .lib already built) you are going to need to build it as a separate project from the code you want to use the library, that step will build the .lib file. If a .sln is included with the distribution use that otherwise you may well have to create your own (or add it as a project to an existing solution).
Once you have a .lib add the directory under the VC++ directories of the project settings and add the actual .lib file name under Linker->Input on the Additional Dependencies line.
To build the library if the distribution does not include the needed VS files you will need to create a project at minimum (it can be part of the solution for your program), right click on the solution node in solution explorer and select Add->New Project, from there select Visual C++->Windows Desktop and either Dynamic Link Library or Static Library as you desire.
Go to the Project menu and select Project Dependencies, change your program to depend on the new project, this will set build order so your program project builds after the library.
You may need to disable the use of pre-compiled headers, right click on the new project node select Properties, go to C/C++->precompiled headers and change Precompiled Headers->Precompiled Header to Not using precompiled headers.
Next add the header and source files to the project and attempt building.
If this succeeded you will have a .lib suitable for use in the additional dependencies of your program project as already described.

How to use C++ libraries?

I am a Python developer and I have not used C++ since university. I am doing scientific programming with python, mainly. I wanted to try C++ to see if it is better performance-wise.
I am quite new in C++. I have found dlib library, which seemed a good library as it had many interesting features. But when I downloaded it, I found several folder full of .h and .cpp files.
In Python, I would have installed a wanted library using pip or something, then use it in my project using import.
Is there a similar installation for c++ libraries? Or do I have to look among all those .h and .cpp files and decide which ones I need in my project then copy them? Or how do I use the dlib library?
I have searched a lot on google but I could not find any indication on how to use a c++ library or install a new package to be used.
I use Visual Studio Community 2017 and Windows 10 if that matters.
To integrate a library, you need two kinds of things:
header files (usually *.h) with the declarations required to let the compiler know about the features in the library (a little similar to an import statement);
compiled library files (usually *.lib) containing the precompiled executable code itself, to let the linker assemble the final executable file.
In some cases (especially for templated code), a library can be made of header files only. In other cases, the package doesn't contain a ready-made library file and you have to build it yourself or manually include the source files (*.c/cpp) in your project.
Not speaking of the innumerable optional settings that you may have to adjust to comply with the specifics of the generated code, such as function calling convention, struct alignment...
Last but not least, some libraries are connected to at run-time only. They are called Dynamic Link Libraries and involve yet different procedures.
All of this is relatively complex and close to black magic for beginners. If you are very lucky, you will find some library documentation telling you which options to use for your compiler. If you can, start from an existing sample project that works!
For dlib, check http://dlib.net/compile.html.
Be ready for a cultural shock when you will compare to the ease of use of the Python modules.
It is quite a broad question, but I'll do my best.
First of all, in C++ libraries consist of header files and precompiled part (.lib,.dll on Windows, .a, .so on Linux). To use them in your project you have to inform your program (and compiler) about features that library has (by #including their header file) and linker to include that library binaries.
pip is package manager, which automatically downloads, builds and installs library that you want in your system. In C++ there is no such single tool at the moment and there steps must be done more or less manually.
For dowloading you usually end up with git or downloading the zip archive with source (do it here). Once you have sources you have to build it.
In order to achieve multiplatformity libraries usually does not get shipped with concrete build system description (Visual Studio Project on Windows or makefile on Linux etc.), but are created in more general tool CMake, which abstracts them. E.g. dlib does that. With use of CMake (For start I recommend CMake-GUI, which is installed with CMake on Windows) you can generate Visual Studio Project, which later you can open and compile to generate .lib file. How exactly to do it follow dlib compilation description.
Once you have you lib and headers files on your disk you can add headers and .lib to your Visual Project and use as any other C++ library. (Seems old, but should work)
As far as I know, there are no tools similar to pip for C++. What you have to do depends on your working environment and the respective library.
In case of dlib there are instructions on the project homepage for Visual Studio. Basically, it involves compiling the whole library alongside your own project by copying a single source file to it and setting up include pathes.
From http://dlib.net/compile.html:
Compiling on Windows Using Visual Studio 2015 or Newer
All you need to do is create an empty console project. Then add dlib/all/source.cpp to it and add the folder containing the dlib folder to the #include search path. Then you can compile any example program by adding it to your project.
Again, note that dlib will only be able to work with jpeg and png files if you link in libjpeg and libpng. In Visual Studio, the easiest way to do this is to add all the libjpeg, libpng, and zlib source files in the dlib/external folder into your project and also define the DLIB_PNG_SUPPORT and DLIB_JPEG_SUPPORT preprocessor directives. If you don't know how to configure Visual Studio then you should use CMake as shown above since it will take care of everything automatically.
You have to download them, put them in your project directory, and then include them almost the same way you would do in python. You need to include only the .h files.
Example for test.h:
#include "test.h"
Hope this helps!

How do I share C++ source code files between projects in Visual Studio?

I'm writing a cross-platform application. One version will work under Win32 and the second on Windows Phone.
I'd like to reuse my C++ core - especially that there are no platform dependencies inside, STL only. I order to do so, I want to use the same source files in two projects: static Win32 library (.lib) and Windows Phone Component (C++/CLI).
How can I configure these two projects to use exactly the same source and header files?
OK, let's take an example. Let's say, that I have project:
MyApp.Library [win32 lib]
myClass.cpp
myClass.h
This library is compiled to .DLL file and then imported in a Win32 application:
MyApp.Win32App [win32 C#]
Since Win32 is not compatible with Windows Phone on the binary level, I cannot use that library directly. But since the library uses only STL, I can create a Windows Phone component, put all its sources there, and build.
MyApp.Component [Windows Phone component]
myClass.cpp
myClass.h
I want these two files to be exactly the same ones as used in the library. How should I organize the project to achieve this effect?
You can add source code from a common location to multiple projects. I do that a lot; I have code in a common directory that is at the same level in the directory hierarchy as most of my project files. It's simply a matter of adding .h and .cpp files from the common directory to your various projects.
I did notice that VisualStudio gets a little cranky and difficult if you use a network drive for common source, so I don't do that. But so long as all of your source code is on local disks, and the IDE knows where to find them, there is no problem having the same source file in very many projects.
one way is to put all .hpp and .cpp files into a seperate folder - call it "Shared".
Then add an additional include Directory inside your solution configuration property - this directory must be a relative path.
Then add ONLY the .cpp files relative from that shared folder into your project
NB! If you have include "stdafx.h" inside your .cpp files in the Shared folder, then comment those out.

How to include a C/C++ lib (e.g. libcurl) in a C++ project (IDE: Eclipse)?

I have a problem, I'm working on a C++ project (I'm using Eclipse as my IDE).
Now I want to make use of a C/C++ library, for example libcurl, so I download curl and don't know what to do next.
Must I only include the .h and .c files I need or must I compile it before?
A typical way to use a library (this, of course might differ from library to library) is to include all of the header files from the library, include them as needed, use functions that they provide. Then, when building the application, link the objects with the library object (.a, .lib, whatever the extension). If you don't have the .a or .lib file, then you should build that independently
You must compile it before, then include the h files in your include path (in the Eclipse project). You will also need to find out how to import the library that you compile into your Eclipse project.
Once all that is done, you can make calls to the library after #include'ing the appropriate headers.
Both ways are possible. Compiling to a library is probably easier.

How to create separate library for include in C++/Eclipse

I've gotten some C++ code to work with the TinyXML parser. However, to do this I had to include the source code from TinyXML with my regular source code. I'd like to have TinyXML included as a separate library. I'm using Eclipse with the Cygwin C++ compiler. What's a good way to do this?
I assume you want to separate the library from your own project's source code... but you don't know how to build when the library is not in the same folder.
Assuming your library has precompiled *.lib and *.h files:
Move the library source code to a separate directory
Menubar "project"
Menu "properties" will open a dialog box for all the project properties there will be a list on the left.
List item "C/C++ Build" will change the GUI and show you all the options for gcc's compiler/linker/assembler ( I never do assembly... so I never do anything with the assembler ). [1]
GCC C Compiler --> Directories:
Green plus icon [2] --> Specify the path of your *.h files
Your compiler should now be happy ( but you will fail linking because the linker doesn't know what the actual definitions of each function are )
GCC C Linker --> Libraries:
Library search path (-L) --> Green plus icon --> Specify the path of your *.lib files
Libraries (-l) --> Green plus icon --> Specify the name of each library you are using
Your linker should now be happy and your code should compile
[Footnote - 1] The GUI C/C++ build pane is a wrapper for gcc's command line compiler/linker... it is just making it easier to use because it shows you everything visually.
[Footnote - 2] The '+' icon is what will tell the compiler where your libraries *.h include files are located. The compiler needs the *.h files to know what function prototypes your library has before it compiles.
Assuming you have the actual ( not compiled ) *.c and *.h:
Do the same steps above except in step 7.
At step 7. you need to make sure the library's *.c files are seen by Eclipse's "managed make". If it doesn't see the source code then you need to specify where the source is so that it will compile it.
It's basically easy. You compile your source code for the library, and construct the library with ar(1). Yes, surprise, a library is just an archive; UNIX is cool that way.
You can then include the code as a static library when you build the final code.
I don't use Eclipse all that much so I can't tell you the exact process within the IDE, but I believe what you need is to set up a separate project to build it.
Now, if what you want is to build a DLL, then you need to use some special flags. There's a nice page here.