Giter Site home page Giter Site logo

Comments (5)

muneebahmed10 avatar muneebahmed10 commented on September 28, 2024

Hi @orkfire,

If you are using C code, then unless it's already compatible with C++, it will need to be compiled as C. Your compiler is likely treating the C input as C++, so you may have to specify the language for your input files. For example, simple programs compiled with g++ can have language specified with -x:

g++ *.cpp -x c *.c

Or, you could compile your C and C++ code separately and then use a C++ linker, e.g.

gcc -c *.c
g++ -c *.cpp
g++ *.o

Additionally, the C code will need extern "C" {} guards to avoid the C++ compiler mangling names when calling C functions from C++. The FreeRTOS kernel header files already include this, but some library files may not, so you should use the format

extern "C" {
    #include "some_c_header.h"
}

when including C header files that don't already have those guards.

from freertos.

orkfire avatar orkfire commented on September 28, 2024

Hi @orkfire,

If you are using C code, then unless it's already compatible with C++, it will need to be compiled as C. Your compiler is likely treating the C input as C++, so you may have to specify the language for your input files. For example, simple programs compiled with g++ can have language specified with -x:

g++ *.cpp -x c *.c

Or, you could compile your C and C++ code separately and then use a C++ linker, e.g.

gcc -c *.c
g++ -c *.cpp
g++ *.o

Additionally, the C code will need extern "C" {} guards to avoid the C++ compiler mangling names when calling C functions from C++. The FreeRTOS kernel header files already include this, but some library files may not, so you should use the format

extern "C" {
    #include "some_c_header.h"
}

when including C header files that don't already have those guards.

I tried using the extern "c" method, but it doesn't help, now I have a method to deal with it, First I using IAR build a static library using the c compiler, and then link it using c++ complier, it can work. but I don't think it is a good method when building a large project.

from freertos.

muneebahmed10 avatar muneebahmed10 commented on September 28, 2024

Hi @orkfire,

Adding extern "C" doesn't automatically tell a compiler to compile specific code as C, it only tells a C++ compiler to not mangle certain C functions in C++ code so that compiled C code can be properly linked. You will still have to tell your compiler to compile C code with a C compiler and C++ with a C++ compiler. In the screenshot you posted, have you tried using the language setting "Auto (extension-based)"?

Additionally, have you taken a look at IAR's notes on mixing C and C++? One recommendation there is

If you want to use C code from your C++ application, consider building the C code in a separate (library) project. You can place the C (library) project in the same workspace as your C++ project for fast access.

from freertos.

orkfire avatar orkfire commented on September 28, 2024

Hi @orkfire,

Adding extern "C" doesn't automatically tell a compiler to compile specific code as C, it only tells a C++ compiler to not mangle certain C functions in C++ code so that compiled C code can be properly linked. You will still have to tell your compiler to compile C code with a C compiler and C++ with a C++ compiler. In the screenshot you posted, have you tried using the language setting "Auto (extension-based)"?

Additionally, have you taken a look at IAR's notes on mixing C and C++? One recommendation there is

If you want to use C code from your C++ application, consider building the C code in a separate (library) project. You can place the C (library) project in the same workspace as your C++ project for fast access.

I tried the auto option, it doesn't work, if I using cmake or makefile, it may be very easy to organize the c and c++ file with a different compiler, but the IDE(IAR) is not that flexible. Maybe I should try using none IDE method to organize my project, os it will be very convenient.

from freertos.

muneebahmed10 avatar muneebahmed10 commented on September 28, 2024

Hi @orkfire,

Do you have any further questions on this, or can we close this issue?

Thanks

from freertos.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.