Import C++ file in C project.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Import C++ file in C project.

1,813 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Polux rsv on Mon Apr 30 06:05:04 MST 2012
Sorry for a dumb question from an "electronics guy" knowing only C and trying to code his projects for LPC1769.

I have created a C project, and I want to import cpp files from a different microcontroller/project :rolleyes:. The compiler stop with error on the first "class" reserved word. Where should I modify the project to let it compile my cpp files.

I could create a C++ project whithin LPCxpresso (4.2.2) and import cpp files. I tried, but there are two different ways, "quickstart->new project" and "file->new project->C++..." These two options are different, and I don't know which one is correct.

Any help is welcome.

Angelo
0 Kudos
Reply
13 Replies

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ben on Tue Jun 12 09:26:49 MST 2012
The toolchain is built with C++ support.  Only the IDE doesn't support C++.  There are two effects of this:

(1) Only files named *.c will be built by the makefile.
(2) The background syntax check will complain about perfectly valid C++ code.

In order to compile a single file as C++, open the Build Settings and under miscellaneous, append `-x c++` to the list of flags.  Remember to use `extern "C"` for any functions that will be called from C code, or else the C++ compiler will mangle names.  The linker will catch most of these, but the startup code uses weak symbols for interrupt handlers, so you need to be extra careful with those.
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Thu May 03 11:05:26 MST 2012

Quote: researchinnovation
@Larryvc....!!!

Hi...!!!

Thanks for your kind suggestion.
I will keep it in mind.
Keep in touch and give me an opportunity to learn and grow in my career.
[COLOR=blue]I am sorry, if I have hurt you in anyway.[/COLOR]
I am just trying to be among stars and gems like you.


Thanks & Regards....!!!:)


@researchinnovation,

Although I have been doing this work for very many years,  I too am still learning and do not feel like I should be considered a "star or gem".  You have not hurt me and no apology is necessary.

I hope you do very well in your studies and career and can see from your eagerness that you will succeed in whatever endeavors you choose to take on.

I apologize to everyone for taking this thread on a tangent.

Sincerely,
Larry
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by stalisman on Thu May 03 00:31:05 MST 2012

Quote: researchinnovation
@Serge....Hi..!!!

Thanks a lot for your positive feedback, I will work on it.

Thanks & Regards....!!!:)



Hi,

I am sure you meant to be helpfull.

Best Regards

Stali

:-)
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by researchinnovation on Thu May 03 00:22:41 MST 2012
@Serge....Hi..!!!

Thanks a lot for your positive feedback, I will work on it.

Thanks & Regards....!!!:)
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by serge on Wed May 02 23:56:11 MST 2012
@researchinnovation

If you want to be among the stars and gems like larryvc, zero and rob65 then you should first read posts from start to end and don't give answers to something the threadstarter refuses to do like go back to Ubuntu if he really wants to use FreeBSD. Secondly think very carefully if your post will be really helpfull. Third try to learn and understand how everything works. It take years to come up to a level of the stars. Even i have to learn and study each day (even with my 30 years of experience) and i am not up to the level of zero, rob65, larryvc and some other stars. I worked for companies like Siemens, Toshiba and a long time ago Olivetti. I think those are some references and i will only post when i can tell something really helping people and only when the topic is in my experience.

Kind regards,

Serge

PS Sorry stars this post is not meant to pollute this thread
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by researchinnovation on Wed May 02 23:31:10 MST 2012
@Larryvc....!!!

Hi...!!!

Thanks for your kind suggestion.
I will keep it in mind.
Keep in touch and give me an opportunity to learn and grow in my career.
[COLOR=Blue]I am sorry, if I have hurt you in anyway.[/COLOR]
I am just trying to be among stars and gems like you.


Thanks & Regards....!!!:)
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Wed May 02 23:26:41 MST 2012
@researchinnovation,

I am sure you mean well.  A simple yes would have sufficed.  I certainly don't need to be taught or lectured to about programming languages.

But to make a blanket statement like you posted, "It should work with few warnings....!!!", is not even close to being correct.

Slow down and take the time think before trying to answer every post that you come across.  This is not a race that we are competing against each other in.  I am speaking from experience as I too rushed to answer posts when I first started on this forum.

Namaste
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by researchinnovation on Wed May 02 21:51:07 MST 2012
@Larryvc.....!!!


Hi.. !!
Are you talking about Polymorphism.
Please go through the following comments on C and C++,

1. C follows the procedural programming paradigm while C++ is a multi-paradigm language(procedural as well as object oriented)In case of C, importance is given to the steps or  procedure of the program while C++ focuses on the data rather than the  process.Also, it is easier to implement/edit the code in case of C++ for the same reason.
  2. In case of C, the data is not secured while the data is secured(hidden) in C++This difference is due to specific [COLOR=Black]http://http://zarrata.com/durofy/programming/the-basics-of-object-oriented-programming/OOP features li[/COLOR]ke Data Hiding which are not present in C.
  3. C is a low-level language while C++ is a middle-level language  C is regarded as a low-level language(difficult  interpretation & less user friendly) while C++ has features of both  low-level(concentration on whats going on in the machine hardware) &  high-level languages(concentration on the program itself) & hence  is regarded as a middle-level language.
  4. C uses the top-down approach while C++ uses the bottom-up approach .In case of C, the program is formulated step by step,  each step is processed into detail while in C++, the base elements are  first formulated which then are linked together to give rise to larger  systems.
  5. C is function-driven while C++ is object-driven.Functions are the building blocks of a C program while objects are building blocks of a C++ program.
  6. C++ supports function overloading while C does not .Overloading means two functions having the same name in the same program. This can be done only in C++ with the help of http://http://zarrata.com/durofy/programming/the-basics-of-object-oriented-programming/Polymorphism(an OOP feature)
  7. We can use functions inside structures in C++ but not in C.In case of C++, functions can be used inside a structure while structures cannot contain functions in C.
  8. The NAMESPACE feature in C++ is absent in case of C.C++ uses NAMESPACE which avoid name collisions. For  instance, two students enrolled in the same university cannot have the  same roll number while two students in different universities might have  the same roll number. The universities are two different namespace  & hence contain the same roll number(identifier) but the same  university(one namespace) cannot have two students with the same roll  number(identifier)
[INDENT]      

[/INDENT] 9. The standard input & output functions differ in the two languages C uses scanf & printf while C++ uses cin>> & cout<< as their respective input & output functions
  10. C++ allows the use of reference variables while C does not.
[COLOR=SeaGreen][B]C++ is an extension of C language. This means that you can not only  use the new features introduced with C++ but can also use the power and  efficiency of C language. C and C++ are no more language for writing  compilers and other languages, these general purpose languages are used  worldwide in every field.
Here is a list of differences between c and c++.

The  main difference between C and C++ is that C++ is object oriented while C  is function or procedure oriented. Object oriented programming paradigm  is focused on writing programs that are more readable and maintainable.  It also helps the reuse of code by packaging a group of similar objects  or using the concept of component programming model.  It helps thinking  in a logical way by using the concept of real world concepts of  objects, inheritance and polymorphism. It should be noted that there are  also some drawbacks of such features. For example using polymorphism in  a program can slow down the performance of that program.

On the  other hand, functional and procedural programming focus primarily on  the actions and events, and the programming model focuses on the logical  assertions that trigger execution of program code.[/B][/COLOR]
[COLOR=DarkRed][B]In C we cannot create a class.
The C++ contains the set of class.
In C++ it having operator overloading and in C it doesn't have.
The C++ is the advance version of C.[/B][/COLOR]
[COLOR=Navy][B]c is topdown parsing.
c++ is bottom up parsing.
c is structured language.
c++ is object oriented language.[/B][/COLOR]
[B]C++ take advantage over following points:
1. Reusability (Inheritance).
2. Maintainability (Because of Data encapsulation code looks more readable).
3. Optimizations(Usage of Templates to increase the development time).[/B]
[COLOR=Blue][B]The main difference between C & C++ is:
1.c is a procedure oriented language but c++ is object oriented.

2.There are  new features in c++ like
abstraction,encapsulation,polymorphism which make c++ more powerful.[/B][/COLOR]
[COLOR=Purple][B]There are a lot of differences between the two...some of these are as follows .....
1)C is a procedural language whereas C++ is an object oriented language.
2)C++ have exception handling mechanism , C does not have.
3)C has stdio.h header for I/O, C++ has iostream.h for I/O.
4)C++ supports inheritance, polymorphism, encapsulation.
5)C has classes in which default is private whereas C has structure where default is public.
6)C++ has templates.
7)C++ supports runtime polymorphism.[/B][/COLOR]
[COLOR=SeaGreen][/COLOR]


Thanks & Regards...!!!:)
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Wed May 02 13:12:06 MST 2012

Quote: researchinnovation
@Polux rsv:-

It should work with few warnings....!!!


Thanks & Regards.....:)



Sorry, but that is a load of ...  Do you know the differences between C and C++?
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by researchinnovation on Wed May 02 04:26:23 MST 2012
@Polux rsv:-

It should work with few warnings....!!!


Thanks & Regards.....:)
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by stalisman on Mon Apr 30 08:21:54 MST 2012

Quote: Polux rsv
Despite it is "not supported", I could create C++ projects and have C++ setup in the project properties->C/C++ Build-> settings-> tools settings.
Probably something will not work during a real build, link, debug,.....

Angelo



Hi,

the problem you are most likely to get is that the C compiler will simply fail to understand any C++ specific constructs and syntax.  It would make far more sense for you to convert from C++ classes to C structures etc.

A lot of people write C code for C++ compilers, but that is because they haven't made the investment in learning C++ properly.

It is almost unheard of for folks to expect C++ code for a C compiler to work.  ( It's like fitting Ferrari Engines into Skodas).

Cheers
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Polux rsv on Mon Apr 30 06:51:25 MST 2012
Despite it is "not supported", I could create C++ projects and have C++ setup in the project properties->C/C++ Build-> settings-> tools settings.
Probably something will not work during a real build, link, debug,.....

Angelo
0 Kudos
Reply

1,666 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Ex-Zero on Mon Apr 30 06:10:53 MST 2012

Quote:

The entry-level product is the [B]LPCXpresso[/B] IDE, which supports C development[COLOR=Red] (not [B]C++[/B])[/COLOR] :eek:

http://support.code-red-tech.com/CodeRedWiki/ProductOverview
0 Kudos
Reply