View previous topic :: View next topic |
Author |
Message |
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
header and source files linking |
Posted: Tue Jul 09, 2013 5:37 am |
|
|
Hello everyone,
I have a question, i want to make my own code library but, theres a problem in the linking of the compiler.
The library is composed of 2 files ( header file.h and source file.c)
in the header file i am putting the declaration of my functions and global variables and in the source file i am putting the definition of functions and global variables.
but the compiler is not linking, and complaining (function used but not defined) any help please!
for example i have a file mylib.h
Code: | #ifndef mylib_h
#define mylib_h
int myfunction();
#endif |
and the c file is mylib.c
Code: | #inlude "mylib.h"
int myfuction(void)
{
.......
} |
in the main function
Code: | #include <device.h>
#include"mylib.h"
void main(void)
{
int a = myfunction();
} |
Thanks in advance,
z3ngew |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Tue Jul 09, 2013 6:47 am |
|
|
Er. Well mylib.h, does not include mylib.c. You have mylib.c, including mylib.h, not the other way round.... |
|
|
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Tue Jul 09, 2013 10:14 am |
|
|
You are also correct, but CCS C doesn't quite work that way. Just quoting "standard" usage is not particularly helpful here. CCS sort of works that way, but mulitple compilation units such as you are trying to use, are a relatively recent addition to CCS which is essentially a single unit compiler. Also they've not worked well for many releases. I am not at all sure what the status of the current compiler is for this.
From this you might think that Multiple compilation units are not popular amongst CCS users. You would be right. One issue is that its rare that its required as most PICs have such limited resources and are so varied that its not practical to have truly common reusable library code as is the expected norm with most other C applications. You cannot write and separately compile source code into object code that can then be linked into any project. It has to be compiled from source almost every time.
In CCS C its not enough to just include the headers in your source. You must include all the relevant source files in your project. See "Multiple Compilation Units" in the compiler help for details. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Tue Jul 09, 2013 2:54 pm |
|
|
Even on a compiler that as standard used multiple libraries, just the header file won't do. You have to tell the compiler to include the library object at the final link stage, and the library would have to be compiled to have the functions as exportable objects.
So what you are doing, would not work with any compiler, not just CCS....
You can do it this way with CCS now, but you then need to compile the library separately, include the lines in the library to say that the function is to be exported, and include the library in the link phase.
Best Wishes |
|
|
z3ngew
Joined: 20 Apr 2013 Posts: 50
|
|
Posted: Tue Jul 09, 2013 5:12 pm |
|
|
Many thanks my friends ,
Sincerely |
|
|
|