Mix c and c++ code

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

Mix c and c++ code

3,956 Views
dimitrissideris
Contributor III

My case is that i want to use a c++ library in an existing LpcOpen project for lpcxpresso 4367.

There is this very useful tutorial for importing cpp code to c:

Standard C++ 

Its clear that in order to compile c code, arm-non-eabi-gcc is needed and most often redlib is used.

To compile cpp code arm-none-eabi-g++ is needed and newlib is used.

I have done some research but some things are not yet clear to me.

1. Can i compile lpcopen projects with g++ and newlib so that i can make the available lpcopen projects c++ projects?

In this way as mentioned here Error message of c++ files mixing in c project. | www.LPCware.com 

I can put C files into a C++ project.

2. If i compile a library with g++ and an lpcopen projcet with gcc and use all the extern methods as described in Standard C++ how can i link them all together?

Any help would be appreciated.

Thanks in advance,

Dimitris

Tags (3)
0 Kudos
Reply
4 Replies

1,907 Views
lpcxpresso_supp
NXP Employee
NXP Employee

This is not an LPCXpresso IDE specific thing. There is a lot of information on mixing C and C++ code around on the web. For instance: "How do I call a C++ function from C?"  at Standard C++

Regards,

LPCXpresso Support

0 Kudos
Reply

1,907 Views
dimitrissideris
Contributor III

My question is how can i make an existing lpcopen project that is a c project to a c++ project so that i can add an external c++ library with ease.

In an existing lpcopen project i changed the libs form redlib to newlib and that worked fine.

Can i use the c++ compiler (arm-none-eabi-c++) that lpcxpresso uses for c++ lpcopen projects when you create a new one to an existing lpcopen project?

I tried to do so but errors occur.

Once again my case is that i want to import an c++ library to an existing lpcopen project. For previous answers i understood that changing existing lpcopen c project to c++ would be the most efficient way to add a c++ library.

It would be very helpful if you could share what steps do i need to achieve this.

1. Change all projects that are involved to newlib (chip and board also)

2. Use c++ compiler to an existing lpcopen project and make this c project to c++ ???????

3. ?????

[EDIT] I also checked the eclipse feature of converting a c project to c++ but in LPCxpresso isn't feasible.

Your answers most time are swift and background prerequisites are needed.

Thanks in advance

0 Kudos
Reply

1,907 Views
lpcxpresso_supp
NXP Employee
NXP Employee

You will need to :

1) Create an LPCOpen C++ project using the Quickstart -> New Project wizard

2) Reconfigure the LPCOpen board and chip libraries to build using Newlib rather than Redlib :

Switching the selected C library 

You might actually want to consider using NewlibNano rather than Newlib too.

https://community.nxp.com/message/630732 

Regards,

LPCXpresso Support

0 Kudos
Reply

1,907 Views
dimitrissideris
Contributor III

Hi,

Thanx for the quick answer.

My case is that i want to use an existing c++ library and use its functionality calling methods inside c code.

Can you suggest the most efficient way to achieve this?

Lets say i want to use i2c example and read values from an i2c sensor.

And inside i2c.c code i want to call a method from c++ library? What should i do

0 Kudos
Reply