Results 1 to 4 of 4
Hi all,
I've been using Linux as a desktop OS for a while now, but only recently started getting into development with it. Sadly, I'm used to Visual Studio holding ...
- 06-04-2009 #1Just Joined!
- Join Date
- Sep 2003
- Posts
- 22
How to link an object file into an existing project in C++
Hi all,
I've been using Linux as a desktop OS for a while now, but only recently started getting into development with it. Sadly, I'm used to Visual Studio holding my hand and doing all the compiling, linking, and building for me.
I have a C++ class consisting of a .h and .cpp file that I want to compile into an object and give that object to a customer without giving them the source code. Then they can include this object in their software and use it.
So far, I was able to compile the class with the following...
g++ -c classname.h classname.cpp
This gives me classname.h.gch and classname.o
My question is, how do I go about including these objects in an existing code base to compile the end application? I just want to give the customer the .gch and .o files, and not the .h and .cpp files.
For example, I have a small .cpp file, which just has a main() and a few calls into the class functions from classname. If I try to do something like...
g++ classname.o tester.cpp -o tester
it complains that classname.h is not found, but I don't want to provide them with source code. Note: tester.cpp would be the customer's code, classname is just the class we're providing them.
- 06-04-2009 #2
Never used pre-compiled header but I found this link:
Precompiled Headers - Using the GNU Compiler Collection (GCC)
...Gerard4143Make mine Arch Linux
- 06-04-2009 #3
Here's a sample in C
Makefile
getsetx.cCode:test: getsetx.o test.o getsetx.h.gch gcc getsetx.o test.c -o test test.o: test.c gcc -c test.c getsetx.o: getsetx.c gcc -c getsetx.c getsetx.h.gch: getsetx.h gcc -c getsetx.h
getsetx.hCode:int x = 0; int getx() { return x; } void setx(int val) { x = val; }
test.cCode:int getx(); void setx(int val);
Now if you call make -f makefile and then delete getsetx.h the executable test will run without incident...The same should hold true for g++...Gerard4143Code:#include "getsetx.h" //to get this to work I had to include getsetx.h first #include <stdio.h> #include <stdlib.h> int main(int argc, char**argv) { setx(123); fprintf(stdout, "ans->%d\n", getx()); exit(EXIT_SUCCESS); }Make mine Arch Linux
- 06-08-2009 #4Just Joined!
- Join Date
- Sep 2003
- Posts
- 22
Thanks Gerard. I actually did something similar to this after my initial post. Using your above example, I would only give the customer getsetx.h.ghc and getsetx.o. They would provide test.c and execute
without any access to getsetx.h or getsetx.c.Code:gcc getsetx.o test.c -o test


Reply With Quote