access .a library data
Hi everyone
I have some data:
const u16 theData[] = { ... };
I have stored this data inside of a C++ file
I then compiled the source into a .o file using this:
gcc -c -o myData.o myData.cpp
Next I made it into a .a library using archive:
ar rvs myData.a myData.o
...ok anyway...now I want to access this data from an app that the .a file is linked to. How do I do this?
I was thinking that I could just declare "extern" variables and access the data this way but it is not working.
Neil
_________________
Wisdom follows...so turn around.
I have tried everything..
The data must remain const:
const u16 theData[...];
this is stored in datalib.a or whatever...
I have tried declaring it as:
const extern u16 theData[...];
extern const u16 theData[...];
I still get an undefined reference from the compiler when I try to link with extern.
Is there any way to access constant data from inside a lib?
Neil
The data must remain const:
const u16 theData[...];
this is stored in datalib.a or whatever...
I have tried declaring it as:
const extern u16 theData[...];
extern const u16 theData[...];
I still get an undefined reference from the compiler when I try to link with extern.
Is there any way to access constant data from inside a lib?
Neil
I'm not sure exactly..but it doesn't work if its not const.
The data is palette and sprite data in raw form. Its for the GBA.
Neil
The data is palette and sprite data in raw form. Its for the GBA.
Neil
There is nothing wrong with constant data,
for global data the best method is to declare it only in a header file using extern and define it in an the implementation/source file (.cpp).
Also when you create a static library with gcc you need to tell it to link that library with your source when you compile an executable/dll using the "-l" flag, you would normally place this library in gcc's library folder otherwise you need to also specify the directory of where your static library is aswell using the "-L" flag.
also you should add "lib" on the front of your static libraries so it should be called libmyData.a not myData.a with this in mind you should do something like this:
1. declare only global data in header:
file: myData.hpp
2. define the data in an imp/source file:
file: myData.cpp
3. create static lib, optionally place library in library folder (omitted here):
4. create test code:
file: test.cpp
5. compile and link static library, if library not in the default library folder then specifiy the directory of where it is aswell:
here "-L." says the to search for the library in the current directory.
for global data the best method is to declare it only in a header file using extern and define it in an the implementation/source file (.cpp).
Also when you create a static library with gcc you need to tell it to link that library with your source when you compile an executable/dll using the "-l" flag, you would normally place this library in gcc's library folder otherwise you need to also specify the directory of where your static library is aswell using the "-L" flag.
also you should add "lib" on the front of your static libraries so it should be called libmyData.a not myData.a with this in mind you should do something like this:
1. declare only global data in header:
file: myData.hpp
extern const int theData[];
2. define the data in an imp/source file:
file: myData.cpp
#include "myData.hpp"const int theData[] = { 3, 5, 6 };
3. create static lib, optionally place library in library folder (omitted here):
gcc -O3 -c myData.cppar rvs libmyData.a myData.o
4. create test code:
file: test.cpp
#include "myData.hpp"#include <iostream>int main() { std::cout << theData[0] << std::endl; return 0;}
5. compile and link static library, if library not in the default library folder then specifiy the directory of where it is aswell:
g++ -O3 test.cpp -o test -L. -lmyData
here "-L." says the to search for the library in the current directory.
You need to use extern const in both the declaration and definition (or at least make the declaration visible in both translation units). The global const object will default to internal linkage otherwise.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement