Debugging another programmers library - c++

I have some other coders c++ library which contains source file, makefile etc. I am interested in understanding this library. i.e. what it does(it is poorly documented), how it does, etc.
One way I have is to include this as a library in my code. But I dont want that as I want to debug the code using netbeans(in order to understand the code flow).
Is there some way by which I may debug the library in order to understand it.
Now when I just merely copy paste the other programs code into my netbeans project. It gives me makefile problems. Is there some other way out?
Also the other programmers library has a lot of errors.

Related

Redefining some standard library parts

I have a C++ library that I wrote and that relies on two other libraries, let's call them libA and libB. Both are C++14-compliant, and so is my library.
So far, so good.
Now here is the tricky part. I need to use that library in a very constrained environment: Intel SGX. I won't enter the details, straight to the problem: Intel SGX's SDK, although being C++14 compatible, makes use of a custom libstdC++. Why you ask? Because in an SGX enclave there are many system call you cannot make, especially I/O: file, console, sockets...
My issue is the following: when I add my library to the source, the compilation issues a huge load of errors, complaining that std::cout doesn't exist, that FILEis not defined, and so on. All these errors come from libA and libB's source code.
As libA and libB are quite huge, butchering their source code to get rid of everything that causes an error seems to me like a crazy idea. Resulting code woule be unmaintainable.
This morning I had an idea: what if I added to the project a header that redefines minimal versions of all that's missing? Some kind of compatibility layer that would, for example, redefine std::cout as a "no-operation" version.
It's the first time I encounter such an issue, and I need the elder's advice. Thanks a lot for any suggestions!
Adding a header is probably not enough. You'll probably also need some source code.
Strictly speaking this is Undefined Behavior, but so is the whole SGX. You're putting yourself in the position of the compiler writer.
It does mean you have to understand a bit more about what's expected. In particular, you need to understand how the libraries you use are packaged. If they're already compiled, and you're getting link errors, then your replacements have to be link-compatible.

How to add an existing lib need to be built to my own project

I'm currently working on a project in C++. I use autotools, I'm new for it BTW, to make the building system. An existing library with a makefile must be used in my project. Because it's a rarely used library, for the sake of portability I need to copy the entire sources in the my project directory and ask makefile to build the library with its own makefile before building my codes. What should I do? I have no clue how to add code in Makefile.am or somewhere like it. Any helpful answers are appreciated.
Two parts:
Build the library as recommended by the library's README or instructions. You might have to choose options to produce an acceptable flavor of shareable library.
To your project, add the output shareable library as a library for inclusion. You probably will also have to add a declarations file (.h) to it somewhere too.
It might be expedient to include the library directly into your project in (IMHO) rare cases. However, resist that urge: It usually causes trouble with symbols (namespace pollution and namimg conflicts) and messes up assumptions about how the interfaces are supposed to work. It also makes source code control more difficult.
With the recommended separation of projects (yours v. library), usually some degree of smarts are needed to conditionally rebuild the project if the library has changed. That can be done with a encapsulating makefile which first calls make the_library then calls make myproject. Or you can manually invoke the proper makefiles which would be more convenient since the library—once it is working satisfactorily—probably won't need any work.

C++ import library by source

I'm new to C++ and wonder if it is good practice to include a library by source code. If it is, what would be the best way to achieve this? Just copying in a subfolder and using include?
In my special case, I have written a small library and I'm going to use it on two different microprocessors. Compiling the library separately, copying all headers and using this "package" seems to be overkill for me.
Compiling the library separately is what should be done.
It's not that overkill either : you're just compiling the .o files for your library, then wrapping them in an archive and handling that archive around.
Normally libraries are used as libraries because it is much easier and comfortable that way. If you are using dynamic libraries (.dll or .so) things get even better because you can replace libraries on the fly and things should continue to work smoothly.
You decided to use code repositories instead of libraries which means probably more work for you. If you are happy this way that's OK, but just make sure you do not break any license, some lgpl packages (like Qt) clearly
require their libraries to be linked dynamically.
The best way to do this: hard to say but in your place I would probably use git and include the libraries as submodules.
Just #includeing source code is a bad idea since it means just to copy the code into your own, things can go wrong that way. For example if there is a static variable somewhere in the library code and the same named static variable in your code you will have a conflict.
Instead you should probably compile the library separately and link it, possibly the same way as you would do anyway (ie you build the library and then you link with that library). But the light weight alternative would be just to compile the additional C++ files and then link the object files together to an executable. Details on how you do that is compiler specific.
There's valid reasons for including the library source in this way, for example if your project needs to modify the library during development it would be easier to do so if the rebuilding of the library is done as a part of the build process of the project. With a well designed build process the library shouldn't have to be rebuilt unless there are actual changes to it.
The value of a library is in part that you link it more often than you compile it, leading to a net saving.
If you control all the source, then whatever build process works best for you is fine.
I agree with πάντα ῥεῖ but I'll also add that the reason it is bad practice is because the compiled library can be stored in your computer in a common location and used by tons of different programs, thereby reducing the amount of data your computer has to store, in memory as well as RAM(if more than one running program uses the same library). An example is openGL which is a library that many games use and is probably already in your system somewhere. If you use windows, software installers link up these libraries to their programs and add them if you don't have them. If you use linux, you will be notified if libraries are missing and prompted to install them. All of that aside, you can, technically use un-compiled libraries but that introduces a number of potential licensing problems as well as additional problems with THEIR dependencies.
By copying source code to other projects and "mixing" it with other source code will stop this library from being a "library". Later on you will be tempted to make a small change in one copy (for CPU) or fix a bug and forget to do the same in the other copy.
There might be additional consideration but you should try to keep the code in one place. Do not Repeat Yourself (DRY) is a very strong and fundamental principal of software engineering with many benefits.

Is it possible to read code of a C++ library and modify it?

A bit of a simple question, though the answer may not be. I am fairly new to C++ and was wondering if it was possible to open a C++ library and see it's code. It sounds like a potentially risky move to accidentally change the core code of the library, but I would still like to see if it is possible. Thank you!
There are too kinds of libraries that C++ can use:
compiled to binary libraries which are linked with linker to your
executable;
headers-only libraries which are just included with include into
your source code
You can "open" headers of headers-only libraries and modify code if you wish (but not recommended).
Also many compiled libraries are open source. You can open source files there. If you want to modify such library, you will need to compile it and link your executable against this modified version.
Yes it s possible to open a c++ library and see its code.
If you want to make changes to any functionality simply create your own version of it giving it a different name, or if you want to add functionality just simply extend the class you are interested in. (read up on inheritance for this).

Wrapping C++ library in XCode

I need some help with wrapping C++ libraries in XCode.
What I want to achieve is to create new library in XCode, import C++ library (I have .a and .h files), wrap it to Obj-C so I can import that library to MonoTouch.
The reason why I do it round way is that when I try to import C++ lib into MonoTouch, because of name mangling I keep getting WrongEntryPoint exceptions. Correct me if I'm wrong but there is no way for me to find out mangled names, which depends on compiler.
Thank you in advance
Correct me if I'm wrong but there is no way for me to find out mangled names, which depends on compiler.
Technically you could. Many compilers share the same mangling syntax, maybe the most useful and long-lasting gift from Itanium ;-)
However it will bring it's own pain (e.g. non-primitive types, other compilers) and maintenance issues as you update your C++ code.
You'll better served by:
writing an ObjectiveC wrapper and use MonoTouch's btouch tool to generated bindings;
writing a C wrapper and using .NET p/invoke to call your code;
The choice it yours but if you think about reusing the C++/C# code elsewhere (e.g. Mono for Android) then using C and p/invoke will be reusable.
I would definitely recommend going the route of wrapping the library in an Obj-C library and using btouch to import the library into MonoTouch. I have recently done this for a C++ library that implemented a Sybase database engine. If you look at my questions you will find quite a few pertaining to wrapping C++ libraries as I posted a few times regarding issues I encountered.
Specifically, you can look at these questions:
Linking to a C++ native library in MonoTouch
Wrapping a C++ library in Objective-C is not hiding the C++ symbols
Application with static library runs on simulator but not on actual device
Undefined symbols when linking PhoneGap static library in MonoTouch
Linker options 'Link all assemblies" and "Link SDK assemblies only" causes undefined symbols in 3rd party static library
I would also recommend, if you are going to go the route of an Obj-C wrapper, that you get btouch to output code and include that in your project rather than including a dll from btouch. From my experience, the code worked more reliably than the dll, although the issues with the dll may have been resolved by now. But take a look at this question regarding the btouch issue:
Exception System.InvalidCastException when calling a method bound with btouch that returns an object. MonoTouch bug?
If you have specific questions/problems in building the Obj-C wrapper then ask them here and post some code and I am sure that I or other members of the community would be able to help you with it.
Bruce, as you assumed, I have problems with wrapping C++ code. After hours and hours of reading and trying, I couldn't wrap the C++ code.
Anyway, I managed to create a simple Obj-C library made of some dummy class, and then import it into another library. That worked fine. However, following same pattern, I included C++ .a file along with .h file (I'm not sure whether .h is mandatory because we can link header files in build options, right??) and when I compiled it, it went fine, the build succeeded, but XCode didn't produce new .a library.
I added linker flags: -ObjC -lNameOfLib
Does C++ Standard Library Type in Build - Linking has to be Static? And Symbols Hidden By Default as well?
It would be great if we could write step-by-step tut, since there are tons of various instructions, but I haven't been able to push it through the end.
I'm confused a bit..
Thank you guys...