How do i use a bunch of .h and .cpp files to create a static library that can be used in another c++ Project - c++

I am trying to learn C++ from some Stanford Course and they have a custom library which contains a bunch of header files(.h) and their associated source (.cpp) files.
I want to create new C++ projects in xcode 6.x to try out code and include those custom header files in my new projects.
I have no freaking idea how to include those files in my project so that i can peacefully #include "blahblahblah.h" and use functions from that header file without issues.
I am new to both xcode, c++(although i know some coding part) and anything else with this process.
Summary:
I have a folder Library which contains a series of .h and .cpp
files.
I have XCode 6.x.x/Eclipse installed.
I want to create a new C++ project and use #include
"OneOfThoseHeaderFiles.h" and then use a function or something from
those header files.
Also assume i have no clue how to run some command line codes that i see a lot of.
If someone can please tell me in the most simple way(preferably step-by-step) i would be so eternally grateful and sing praises to you until the day i die :)
Note: Either XCode or Eclipse is fine. I really just want to start coding.
I have spent the better part of a whole week trying out xcode, eclipse etc and trying to follow a ton of steps to get it working but i just cant get how to add the files without issues. Either the info seems to be for Xcode 4.x and every menu seems changed or the suggestions are so complicated that a novice like me cant figure it out.

First off, almost all the information for Xcode will be available on the Apple Developer's portal:
https://developer.apple.com/library/prerelease/ios/documentation/ToolsLanguages/Conceptual/Xcode_Overview/start_project.html#//apple_ref/doc/uid/TP40010215-CH2-SW5
To create a simple static library on Xcode 6 you start by creating a new project, then under OS X - Framework and Library select Library, on the next window change the Framework to None (Plain C/C++ Library) and static (since that is what your are stating).
Once your project is created, add the existing headers and sources to your project, either by dragging them with the mouse or with the add files drop menu from the right click functionality, you can create a new group in Xcode if you want some order for them.
When adding the files, on the add window shown you can select to either copy them or use their current location.
If needed you might have to change the "header search path" in your target configuration under "Build Settings", if it is the first time you are using Xcode some of this will sound weird but I am sure you will find most of it on the Apple Developers portal in great detail.
If you want to test the files without having to create an external static library you could create a command line project and test them faster and easier.

Related

Xcode & C/C++ project

I want to use Xcode for C/C++ project.
I don't care about build/compilation, all i want is to use it as editor.
Needs:
- Find symobol , References , callers etc
I've opened a console project, i can search for a symbol in the same file but nothing else. (for example, right click + jump to definition gives me a question mark ('?').
Any pointer to what needs to be done ?
Thanks,
Shaul.
From my experience, XCode is not designed to work well with C++. For example, you'll find that simple refactoring, such as renaming a function and automatically renaming usages, doesn't work well with XCode. I find AppCode works a lot better for (Obj)C++ development. Worth a look.
Thare are two approches
You need to create a library of your c++ project and add that library in your project
Add C++ project in your xcode and make it build as target source
let me now if you need a walkthrough
Ok, I found the solution.
When adding files to the Xcode project, on the bottom , need to click 'Options' and then choose 'Create groups' instead 'Create folder references'
This options will add all the sources and headers to 'Compile sources' and 'Headers' (Under 'Build Phases')

Visual Studio - unsure about exporting projects

I'm new on here, but not a total beginner to programming. I got a book on creating a 2D DirectX game engine so I could expand my programming knowledge, however I am new to C++ and DirectX itself, so I'm afraid I'm a bit clueless on working with multiple project files and headers. I decided to develop my game engine in a proper development environment, so I got VS2013 Professional though the Dreamspark program.
As I said, I haven't worked with something like VS before, so I'm hitting issues with getting my game engine running and I'm not sure what to do. The book said that if I get all my code down correctly (in 3 .cpp source files, 3 headers and 1 main .cpp source file to initialize my engine in a blank game project) the program should compile fine, and it does. The issue is that I only get the resultant .obj files and the project's static library file to appear in my project folder. I don't know what I have to do to get the project to compile the executable needed to actually run this 'blank game' with the engine.
The actual project solution was taken from the disk which came with the book, I wrote my code on top of that solution as my own was causing linker errors. The project properties show that the project is defined as a .lib project, so how is this supposed to work?
I apologize if this is a simple problem, but I'm only used to working with 1 file console applications in C, this is a big jump for me but I'd like to understand how I'm meant to get an .exe created from this project, I would appreciate it if someone could explain to me how this sort of .lib project works. I can post the code if it's necessary, but there's 7 files of it so it's quite long. Thanks!
Solution 1:
As the project contains a main.cpp it looks like it is not meant to be a library-project.
In VS2013 you can switch the type of a project by
In your Solution Explorer (on the left side by default) rightclick onto the project
Select the item Properties from Context Menu (should be on the bottom)
Select the category General
In the section Project Defaults change the Configuration Type to Executable (.exe)
Solution 2:
It could be possible that the executable is stored somewhere else after a successful build. Check what is configured as output directory:
In your Solution Explorer (on the left side by default) rightclick onto the project
Select the item Properties from Context Menu (should be at the bottom)
Select the category General
In section General there should be an Output Directory defined (At the top of the configuration). Per default it should be something like $(SolutionDir)$(Configuration)\
I'm using VS2013 Express, but it should work with Prof too. I'm also using a German edition, hope I translated everything well.

Is it possible to export a project directly from visual Studio into it's own folder?

I am currently trying to build .net bindings for a very large project that is written in C an C++. The project was recently ported over to Windows, and I might add that is is a big giant mess.
The solution has about 12 projects, which is fine but the directory itself has alot more files than needed,since it includes OS specific source files, demo projects etc.
Also the other thing is the headers and source files are scattered all over the place. I am working on a parser to help me create the bindings for this project, but I am having a heck of a time. Especially when the internal include references are completely off.
The developer did a great job porting this large project to Windows, but isn't very organized and it is driving me bonkers with the issue my parser is having. So the easiest thing to do would be to re structure and put each project in it's own areas.
So as I asked in the title, is there somewhere that I can export each project and it's files from VS 2010(or another tool) so I can begin restructuring the folders? When I have ever needed to do this before in .net it was easy enough to just move the files. In smaller c++ projects I just open up the project file in notepad++ and move each file in the include/source list into different directories and changed the paths in the project file after. The issue with that is it will take way too long, and I have already spent days messing with this project, finding the best way to wrap it, and creating my helper tools.
Can anyone give me some assistance in this aspect?
EDIT:Just to clarify, I would like to export each project into it's own root folder with one folder for headers and one for source files so I can keep things simple.
I'm not sure about a tool for this, but the .vcxproj project files are in XML format, so you can easily extract a list of files from each project. Then you could relocate those files and write their new location back into the project.
Whenever I encounter a serious project mess, I usually end up dragging all the project files into Notepad++ and start doing global search and replace.
I haven't done it on the scale that you seem to be aiming at, where it involves relocating all the sources. You could be in for a rough ride. Fixing up include paths etc could be quite a pain.
I suggest you obtain the file lists (relative paths or whatever) in a simple text format, and then decide whether you want to take the red pill or the blue pill =)

Xcode: multiple projects, more than one main executable

Hi I'm new to Xcode. I'm trying to learn c++ and I was wondering if it is possible to have multi projects and choose which project should run. I used ms visual studios before and I like how under a solution I can create multiple projects and choose which one is the executable one. Xcode also has targets not exactly sure what they are maybe they play a role? I tried messing around but I constantly have issue because of duplicate mains. I'm going through a chapter and there are case studies. I'm trying to keep every chapter organized in one project well something like that. Any help thanks!
Xcode targets are probably what you are looking for. You can have many targets in a single project, and whichever one you select as active will be used when you tell Xcode to compile or run. If you are having problems with duplicate mains, you probably have all of your .cpp files in all of your targets. If you have main1.cpp and main2.cpp in the same target, Xcode will attempt to use both of them and run into a conflict. Including all common files in all targets, but only the appropriate main files will likely solve your problems.
for creating a new main.cpp file you should go to
File-New-target.
After it gets created just select your file here to choose which file you want to execute:
Actually you can create a workspace, then create each exercise as a project or as a target inside a root project, the second option is preferred if the exercises need to share the same libs and setup.
To select the active scheme to debug and run you can use this selection menu close to debug controls.

Beginner questions regarding to "building a library", in Xcode on iPhone specifically

I have never been clearly understand all these linking/building/dependency business. Now, I am trying to build the FreeType library (which is in C++), into the *.a library file for the iPhone (because another library I am trying to use, openFrameworks, would depend on FreeType).
I know that to compile C++ with iPhone I simply need to rename *.cpp to *.mm. (I have tried with some simply programs in main() and it works) But how can I build the library in Xcode then ? (without the main() I suppose)
On the other hand, it would be great if you guys could recommend some books or documents on such linking/building topics, which I found myself most confused about.
PS. I have already got the paid version of Xcode and some sample apps compiled onto the iPhone.
Renaming .cpp files to .mm would mean they'll be treated as Objective-C++ code rather than C++ code. I don't think that'd be a good idea, even if it should still work. Besides, FreeType is written in C, not C++.
Google for "compiler linker" and you'll find quite a few documents on how they work. That should help for documentation.
It's entirely possible to compile static libraries for the iPhone; what you can't do is compile dynamic libraries or frameworks - you could, but it's not encouraged.
Open your project
In the source/target browser, select the "Targets" node (with the little red and white target icon).
Right-click on the node, and in the popup menu select "Add", followed by "New Target".
A dialog opens. On the left hand side, there's an iPhone OS and a Mac OS X section. The iPhone OS section should already be selected; if not do so.
You should have three choices of targets, one of it is a static library. Select it, click OK.
Give the library a name in the next page. Click finish.
Your "Targets" node has includes a child for your static library now. You can add sources either via the file menu, or by dragging it onto the "Compile Sources" child node.
Hope that helps.
First, you do not need to rename any files to compile C++ code for the iPhone. Secondly, you'll want to create a separate project for the library. It looks like the iPhone target types don't include "static library", so you might need to create a Mac OS X project and modify it to output an ARM .a file. Not trivial, but probably doable.
Alternatively, if you're not going to be using this code in a lot of otherwise unrelated projects, you could include the source files directly into your App project. Collect them all into their own group, and you'll hardly know they are there.