Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Never used pre-compiled header but I found this link:

    Precompiled Headers - Using the GNU Compiler Collection (GCC)

    ...Gerard4143
    Make mine Arch Linux

  3. #3
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714

    Here's a sample in C

    Makefile
    Code:
    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.c

    Code:
    int x = 0;
    
    int getx()
    {
    	return x;
    }
    
    void setx(int val)
    {
    	x = val;
    }
    getsetx.h

    Code:
    int getx();
    
    void setx(int val);
    test.c

    Code:
    #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);
    }
    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++...Gerard4143
    Make mine Arch Linux

  4. #4
    Just 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

    Code:
    gcc getsetx.o test.c -o test
    without any access to getsetx.h or getsetx.c.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...