C/C++ Code and LIBRARY Mixing - c++

I’m a native C++ programmer never knew anything about low level C, due to some circumstances I had to use C extensively. The outcome was me catching on a lot of C habits.
Know I’m trying to develop some Computer Vision project using OpenCV, TBB, OpenCL and OpenGL.
The thing is OpenCL is native C, so is TBB I guess not 100% sure about the TBB. As everybody knows C++ is an industrial Language and so Developing in it is a blast(at least for me) but after doing the main Which language should I use research? I’m know more concerned with C/C++ Code MIXING….
So my question is this Should I Develop the whole thing in C (Hard to maintain, expand and develop with) or should I stay with C++. And if I’m staying with C++ can someone plz direct me to a good way, strategy(Book, Tutorial) of mixing C/C++.
Thanks in advance.....

All the technologies you mention have C++ APIs. So if you are already comfortable using that, by all means do so. There does not need to be any mixing of C and C++ in one file, at all.
Only the OpenCL kernels are written in plain (OpenCL-) C. You do not mix the code, but you may have to pass data from your C++ code to your OpenCL kernel. Since you mention you now know both, that should not be a problem.

This C++ faq lite entry should be helpful. Mixing C and CPP

Related

C or C++ for a Robot?

Greetings,
I am trying to decide between C and C++ for my robot. I am a 5+ year veteran of Visual Basic.NET, however I'm going with Linux (Ubuntu) on this robot, and there is a compatibility problem between Linux and the .NET Framework. I want to stick with whichever language I choose for all of my projects, so I want to make sure that I choose the most appropriate one for the task.
For reference, I will describe my current robot in progress and what I am going to do with it. I am in the process of building a full-sized R4 Astromech (yep, I'm one of those guys). I have incorporated a PC motherboard with an Intel Core 2 2.1 GHz processor, 1 GB RAM. I will be using a scratch-built parallel interface card to control the drive motors, head motor, as well as a secondary parallel interface card (going to a second parallel port) which all of the sensors (IR, Ultrasonic Ranging, Visual Recognition via webcam, etc.) will be going to. Commands will be given using speech recognition (currently have a VB.NET scratch-built recognition program that I will be adapting to the new language).
Given the specifications and desired goals listed above, would I be better off with C or C++? I greatly appreciate any suggestions that you might have.
Thanks!
Thaskalas
What do you mean by a compatibility problem? Have you looked at Mono? It's an open-source implementation of the .NET libraries. It's geared toward C# not VB.NET but if you're more comfortable in a .NET environment use that. Speed isn't really an issue here as a Core2Duo is plenty fast for what you need to do.
If Mono won't work for you, I'd recommend C++. There are a lot more libraries out there for C++ (or at least, I am familiar with more, e.g. Boost), which can use most C libraries too. There's no real speed penalty for using C++. While using C wouldn't be bad per-se, C++ has some benefits and no drawbacks, so it's probably the better choice.
I would recommend using ROS. It will let you get started with a sophisticated Inter-Process Communications manager, as well as a large library of sophisticated robotics code, including multiple implementations of SLAM and other critical robotics algorithms. ROS also lets you program in multiple languages, including C, C++, and Python, so you aren't stuck with one language or another down the road.
I would also recommend C++ and ROS. In our company we're migrating to it, because there's so many people working on it, expanding it, and adding lots of cool features.
With this, you can forget about implementing most of the basic low-level stuff and start working on what you intend to research.
It's really easy to set up and start developing.
Since you're running Linux on it, I'd recommend a split approach, where you do the lower-level (device interface, where you may need fast performance) stuff in C (or C++), and the higher level stuff in a modern language like C# (using Mono) or Java, or even Python.
Python especially is hugely expressive, has a large set of libraries, and has a pretty straightforward C interface.
Writing your high-level control stuff in a low-level language like C/C++ will get old fast (IMHO). Robots should be fun!
Have you considered D? It's a fairly new language, is compiled to native code and can link directly to C. (The entire C standard library is even available from D, and bindings to the POSIX API are included in the standard library.) Basically all you need to do to use any C library from D is compile it with a C compiler and translate the function prototypes, constant declarations, etc. in the header file.
D is low-level enough that an experimental kernel is written in it, but has modern features like garbage collection (though manual memory management is still permitted), builtin strings and arrays, and more advanced/easier to use template metaprogramming facilities than C++. The biggest disadvantage is lack of a mature toolchain and libraries for enterprise-y things, but for your purposes that probably doesn't matter. BTW, if you need to do a bunch of matrix manipulation and stuff, there's the SciD project, which provides nice templated wrappers over LAPACK and BLAS.
Use C++. You have the space. You can use it "as a better C" to start with.
C++ is a bigger tool bag; why would you not want that!? You need not use all the tools, but with C you'd have no choice. Most importantly with C++ you have the choice of using both C and C++ third-party libraries.

OpenCV Image Processing -- C++ vs C vs Python

I was thinking of trying OpenCV for a project and noticed that it had C, C++ and Python.
I am trying to figure out whether I should use C++, C or Python -- and would like to use whatever has the best OpenCV support.
Just from looking at the index page for the various documentation it looks like the C++ bindings might have more features than the others? Is this true?
If C++ has more bindings, it seems that would be a more obvious choice for me, but I was just curious if it really has more features, etc than the others?
Thanks!
Actually Python binding was finished starting with release 2.2. So i would go with python if i were you, because the performance will be almost the same anyway as you are using c libraries in both cases. However python will get you there faster, safely and you will spend more time on your algorithm rather than getting things to build and run not mentioning memory management hassles
The Python interface is still being developed whereas the C++ interface (especially with the new Mat class) is quite mature. If you're comfortable in C++, I would highly recommend using it - else, you can start using Python and contribute back any features you think OpenCV needs :)
I think it depends how proficient you are in C++. The Mat interface does appear more modern than the old IPLImage C interface. The problem I'm having is that most of the examples you'll find on the web, or even here on stackoverflow are for the C interface (e.g. cvCvtColor), not for the C++ interface. So I'm really struggling to port the C examples to C++.
Even if you're very proficient in C or C++, you should use python to speed up your development (I should guess a 4x factor). Performance are really quite the same.

Learning C++ Or C

My question is: Is learning C++ without learning C enough to program any kind of computer programs and get the computer to it`s maximum level (Full Control except the tasks that need Assembly language)?
Thank you
Yes, there is no point in learning C first if you want to learn C++. They are two different languages and learning C first is not a requirement.
Everything you can do in C you can do in C++ (probably in a safer way too)
I think the important question to ask is 'What do you want to do?'
There are many tasks and situations where neither C or C++ are exactly appropriate. They also present quite a steep learning curve and do not lend themselves to fast results.
If you are starting out I would recommend a simpler language such as Python (or even PHP for web stuff) but don't take my word for it and ask about - form your own opinion.
Knowing C is useful as it lets you know in greater depth what the computer is actually doing, but for the sake of productivity (and your sanity) it's probably best going with something a bit more high-level to start with.
This reminds me of the question "Should I learn driving with manual transmission (stick shift) to drive a truck or automatic transmission to drive a car". Different skill sets and usage really. With C you "think" procedurally. With C++ you "think" of Objects. Your entire program is structures differently. True, the "syntax" of C++ is a super-set of C but these are really two different languages. Because most C++ compilers also compile C code, there is a common misunderstanding that C++ is just an extension of C. This is only true about the syntax not the concept. For example, you use the same alphabet to write in English and French but the languages are very different.
I hope this helps.
C++ is a great place to start and yes, you can do everything with it. There's no advantage to learning C unless you are planning on working on platforms where it is particularly well suited, such as embedded electronics.
If you master C++ as a language and the OO concepts behind it you will have no difficulty picking up any other programming language. What's more you'll have a much better appreciation for memory management than you would get if you started with a slightly higher level language such as Java.
Good luck.
I think it all depends on the platform you wish to develop for.
If you want to develop applications on the Mac or iPhone you need to use the Cocoa and Cocoa Touch frameworks which are (mostly) written in Objective-C.
If you want to develop games for most consoles you need to use the native frameworks which are almost all written in C++.
I'm not sure what the Windows frameworks use, but I'd guess at C++.
The language you use is secondary to the frameworks you'll need to use for the platform you're developing for.
Yes, C++ is (pretty much) a superset of C
C++ is include all of C features and more other features like the support of OOP, so the answer is Yes you can.
it's different languages..
chose depending that you want to do...
Yes, you can start learning C++. Start with non-OOP part of C++, which is similar to C. Then you can move to the OOP part. Most of the books teaching C++ from ground up follow this approach.
me just learn c++ but
no problem in understanding c
c++ is much more supportive and safe than c.
although it is a puls puls version...
C++ is all that C is, and then more, so using C++ will not prevent you from doing anything with the computer that you can do in C (although it will in some cases prevent you from writing what in C would normally be an error in any case because C++ compilers use stronger type checking).
Now with respect to "Full Control"; there are some tasks that are the responsibility of the OS, and modern OS's will prevent you from having full control at user level (because processes have to play nicely together). Of course if you are coding for an embedded target, or writing a bootloader or OS, or a kernel mode driver, then you do have full control; and that is why C and C++ are used predominantly for such tasks because they are intrinsically systems-level languages.
I think any language is just medium of expressing one's logic, the main thing is your logic which needs to be perfect. First of all you must understand what are the features that could be explored in C++ that are not available in C.
C is known for its simplicity. If you start learning C, you will very well understand that how crisp and clear this language is. However, it is not a compulsion that you should learn C first and then C++; Its just a recommendation.
Learning C first is like playing level 1 of a game, where C++ is level 2 :-)
Having said that you can always play level 2 directly and can equally succeed in that, but with a little difficulty. :-)
I started by learning C++ and it was fine.
However it wouldn't hurt to have a browse through C before diving into C++. Loosely speaking C is a subset and you will learn the fundamentals there. C++ tends to have some tricky topics to grasp so you'll be safer by having strong foundations.
If you are starting programming, I would say start with C. It will give many basics and will let you think in a more low-level way than starting C++. You do not need to be the boss in C, just get the basics that will help in the future.
There is a nice article written by Joel Spolsky (joelonsoftware.com) that talks about how to start.

C vs C++ (Objective-C vs Objective-C++) for iPhone

I would like to create a portable library for iPhone, that also could be used for other platforms.
My question is the fallowing:
Does anyone knows what is the best to be used on the iPhone: Objective-C or Objective-C++? Does it works with C++ the same way as Objective-C with C or not?
Reasons: Objective-C is a superset of C, but Objective-C++ is not a superset of C++.
Thanks in advance!
UPDATE: What about memory usage, speed in the same implementation of an use case?
UPDATE1: If anyone can provide any more information, he'll be welcome.
If you're writing a portable library, and don't need specific language features for it, why not write it in straight C? It can be easily called from C++, C, Objective-C, and many, many other languages.
If you require specific language features that aren't available in C, you should tell us what they are so you can get better advice =)
I believe your information is incorrect. Objective-C++ is a superset of C++. Any C++ is legal Objective-C++. In addition, you can mix Objective-C and C++ code in an Objective-C++ (usually .mm) file and (with some restrictions) mix Objective-C and C++ class instance variables within an Objective-C++ class. Objective-C++ is particularly useful for interfacing between Objective-C and a C++ library. Write your cross-platform library in C++. You can then call it from Objective-C++ within an application. Re-read the Objective-C++ section of the Objective-C language guide for more info.
The major downside to using Objective-C++ is increased compile times (Objective-C++ is even worse than C++ and the Clang LLVM compiler doesn't handle Objective-C++ yet). There is no performance difference between Objective-C and Objective-C++ beyond any differences in the code that's called (e.g. if you're a better C++ dev., your C++ will probably be more efficient than your C library and visa versa).
They're not really different languages. Objective-C++ is just Objective-C with slightly limited support for including C++ code. Objective-C is the standard dialect, but if you need to work with C++, there's no reason not to use it. AFAIK, the biggest practical difference (aside from allowing use of different libraries) is that Objective-C++ seems to compile a bit slower. Just be sure to read up on it first if you do decide to go that route, because the merging of C++ and Objective-C is not 100% seamless.
If you are going to use this library on platforms other than the iPhone and Mac OS, you should use C++. If you plan only to port it to Mac OS, then go ahead and use Objective-C. Objective-C++ is just a way to integrate existing C++ code into Objective-C projects. Thus, you would not write a library in it. You might, however, use Objective-C++ to use this library from an iPhone or Mac project.
The truth is you can use C or C++ in Xcode. The reason to use Objective C for Cocoa and Cocoa Touch (iphone) is because of it's ease of use and how it handles many of the memory issues you would come across on it's own. Objective C and Xcode work like a dream. I would also recommend checking out http://www.kevincallahan.org/software/accessorizer.html
Accessorizer is amazing and cuts development time into nothing.
Having said that I see no problem in writing functions/methods in C or C++ and using them within a part of your XCode project. Just do not expect to use Objective C to play as nicely on other platforms. Apple and Windows development IDE's are different and Microsoft has a nice habit for not making things as compatible with other environments (not just Apple.)
I am glad to hear someone out their trying to reuse their code for other projects : ) Too many people forget to create their own libraries these days and I haven't the slightest idea why.
I hope this helps. Cheers,
Matthew
Here is your caveat: Spend the hundred on becoming a developer and launch xcode the IDE you will be working with on the Mac - this would be the best way for you to decide as it is apparent that you do not fully understand how this platform works at all.
Please don't be rude to everyone trying to help you. This community is built on respect. The people here really actually do help out and care and there are few places like this on the net left.
Go for C or C++ - I do not see any reason for recommending one over the other without knowing more about your goals.
In terms of memory usage, C++ and C do demand more disciplined coding. In terms of speed, C and C++ will in virtually any case be faster than any ObjC/ObjC++ solution. I do frequently mix my Objective C applications with C and C++ code and was always pleased with the results. As Jonathan correctly stated, ObjectiveC++ is mostly a bridge for using C++ code/libraries within your Objective C application/s.
You can freely use C++ objects from within Objective C++. You can however NOT use Objective C/C++ objects within C++. So its a one-way-road. To overcome any limitations imposed by this restriction, simply wrap your C++ objects with Objective C++ classes where needed.
"Objective-C is a strict superset of C, and Objective-C++ is a strict superset of C++." - https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/CppRuntimeEnv/Articles/SymbolVisibility.html

Is it possible to program iPhone in C++

I'm all for language diversity, but Objective C is insane. So I'm curious: is it possible to code iPhone apps with C++ while using the Cocoa API, etc?
Although Objective-C does indeed appear to be "insane" initially, I encourage you to stick with it. Once you have an "a-ha" moment, suddenly it all starts to make sense. For me it took about 2 weeks of focused Objective-C concentration to really understand the Cocoa frameworks, the language, and how it all fits together. But once I really "got" it, it was very very exciting.
It sounds cliché, but it's true. Stick it out.
Of course, if you're bringing in C++ libraries or existing C++ code, you can use those modules with Objective-C/Objective-C++.
Short answer, yes, sort of. You can use Objective-C++, which you can read about at Apple Developer Connection.
If you know C++ already, learning Objective-C would be pretty simple, if you decided to give that a try. More info on that topic is at the ADC as well.
I use Objective-C to slap the UI together.
But the hard guts of the code is still written in C++.
That is the main purpose of Objective-C the UI interface and handling the events.
And it works great for that purpose.
I still like C++ as the backend for the code though (but that's mainly becuase I like C++) you could quite easily use Objective-C for the backend of the application as well.
First off, saying Objective-C is "insane" is humorous- I have the Bjarne Stroustrup C++ book sitting by my side which clocks in at 1020 pages. Apple's PDF on Objective-C is 141.
If you want to use UIKit it will be very, very difficult for you to do anything in C++. Any serious iPhone app that conforms to Apple's UI will need it's UI portions to be written in Objective-C. Only if you're writing an OpenGL game can you stick almost entirely to C/C++.
Having some experience of this, you can indeed use C++ code for your "core" code, but you have to use objective-C for anything iPhone specific.
Don't try to force Objective-C to act like C++. At first it will seem to you this is possible, but the resulting code really won't work well with Cocoa, and you will get very confused as to what is going on. Take the time to learn properly, without any C++ around, how to build GUIs and iPhone applications, then link in your C++ base.
You have to use Objective C to interface with the Cocoa API, so there is no choice. Of course, you can use as much C++ as you like behind the scenes (Objective C++ makes this easy).
It is an insane language indeed, but it's also... kind of fun to use once you're a bit used to it. :-)
I'm not sure about C++, but you can definitely code iPhone applications in C#, using a product called MonoTouch.
You can see this post for detailed discussion on MonoTouch Vs Obj-C: How to decide between MonoTouch and Objective-C?
I'm in the process of porting a computation-intensive Android app written in Java to iOS6. I'm doing this by porting the non-UI parts from Java to C++, writing the (minimal) UI parts in Obj-C, and wrapping the former in a (small) C interface using the standard C/C++ technique, so that it can be accessed from Obj-C, which is after all a superset of C.
This has been effective so far, and I haven't encountered any gotchas. It seems to be a legitimate approach, since Xcode lets you create C++ classes as well as Obj-C classes, and some of the official sample code does things this way. I haven't had to go outside any officially supported interfaces to do this.
There wouldn't seem to be much to gain from writing my remaining UI code in C++ even if it were possible, in view of the help given to you by the interface builder in Xcode, so my answer would be that you can use C++ for almost all your app, or as much of it as you find appropriate/convenient.
Yes but Thinking that you can program every kind of program in a single language is a flawed idea unless you are writing very simple programs. Objective C is for Cocoa as C# is for .NET, Use the right tool for right job, Trying to make C++ interact to Cocoa via writing bridging code and trying to make C++ code behave according to Cocoa requirements is not a good idea neither expecting C++ performance from Objective C is. You should try to layout design and architecture of app keeping in view existing skills and determine which part should be written in which language then build accordingly.
I'm currently writing an Objective-C++ framework called Objective-X, wich makes PURE C++ iPHONE PROGRAMMING possible. You can do like this:
#import "ObjectiveX.h"
void GUIApplicationMain() {
    GUIAlert Alert;
    GUILabel Label;
    GUIScreen MainScreen;
    Alert.set_text(#"Just a lovely alert box!");
    Alert.set_title(#"Hello!");
    Alert.set_button(#"Okay");
    Alert.show();
    Label.set_text(#"Ciao!");
    Label.set_position(100, 200, 120, 40);
    MainScreen.init();
    MainScreen.addGUIControl(Label.init());
}
and compile it using GCC's appropriate commandline options. I've already compiled this helloworld app&it w0rkX0rz like a charm. ;-) It'll available soon on GoogleCode. Search for Objective-X or visit http://infotronix.orgfree.com/objectivex approx. a week later!
Updated (but apparently inactive) URL: http://code.google.com/p/objectivex/
It may be a bit offtopic, but anyway. You can program c++ right on iOS devices. Check out CppCode ios app - http://cppcode.info. I believe it helps to learn c and c++ and objective-c later.