Sign in to follow this  
noaktree

access .a library data

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

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.cpp

ar 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this