This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

MSP430FR2355: Is the following use of extern with respect to k legal in the following example?

Part Number: MSP430FR2355

Assuming I link the library correctly to the executable project can I define k in the library then use in the executable as seen below?  Is this legal?

Project A (library): libtest.libc

libA.c

#include "lib.h"

int k = 0;

libB.c

#include "lib.h"

m = k+1;

lib.h

extern int k;

Project B (executable): main.c

#include "lib.h"


r = k*2

Thanks

  • Yep, that is the way to do it.

  • Part Number: MSP430FR2355

    A trickier question and the use of static....this is hypothetical and I'm trying to understand. If I have a lib.libc project with multiple c source files and I place 

    static int k = 0;

    at the global scope level of one of the c source files because I want the value k to be retained not only across functions within the source file but across source files within the project.  How will the second source file in the library see this?  Do I use static and extern together somehow or should I be placing the definition in an header file for the project?

  • Unfortunately, the meaning of static depends on the context.

    It is rarely needed in global definitions out of a function scope.

    It really just matters in a function definition to state that its value should be retained between calls. This is what automatically happens for global variables.

  • If however you have multiple functions within a file and use static outside these then they all have access to it AND no one outside the file can see it, correct?

    If so my question I guess becomes if you have multiple c source files and a header within a library project, what is the best mechanism for creating 'private' variables SUCH that all the library source files can see them and use them BUT the application code project will not see these variables (or not have access to them) once you include the library header file into that executable project?

  • Hi Steve,

    I'm reading along here and wanted to know if you got your question answered?

  • Hi Dennis....

    Yes I believe all is good here....

    Thanks

    Steve

**Attention** This is a public forum