Thread: How to reuse a certain function in a large C project

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    2

    How to reuse a certain function in a large C project

    Hi all

    I want to ask what I should do to conviently reuse(include, compile and build executable) a certain function in a large C project that doesn't expose it

    For example, I want to use function defined here postgres/fe-auth-scram.c at master * postgres/postgres * GitHubin this way:
    Code:
    // mycode.c
    
    #include <stdlib.h>
    // what else should I include?
    
    int main() {
         char* pass = (char*) malloc(sizeof(char) * 1000);
         pass = pg_fe_scram_build_secret("testpassword");
    }

    I'm really a beginner in C and work on Linux platform
    only know there is compile, link procedure when using gcc(not learned compilation theory)
    but don't know how to design a gcc command that could let me write less code to reuse that function

    thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,925
    The function is declared in fe-auth.h, so I don't see what's the problem: just include that header and link appropriately.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    2
    Quote Originally Posted by laserlight View Post
    The function is declared in fe-auth.h, so I don't see what's the problem: just include that header and link appropriately.
    sry, my bad for not making this question clear

    so my way of doing this now:
    1. download from PostgreSQL: File Browser2. unpack it and ./configure then make
    3. now write my code as
    ...
    #include "fe-auth.h"
    const char* pass_hash = pg_fe_scram_build_verifier("testpassword");
    ...
    4. gcc -c mycode.c -I all_the_includes
    5. but I don't know what to link to
    a gcc -o result mycode.o
    would throw a lot of undefined errors


    what sould I do then?

    use vscode find in file to find definition and relevant .o then link them one by one?
    any tools to do it automatically for me?

    thanks

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    721
    First: Read the INSTALL file...

    Check what header file declares the function in INCLUDE_DIR
    Check which library to use in LIB_DIR

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Starting a large project, tips?
    By Alpo in forum General Discussions
    Replies: 28
    Last Post: 10-27-2014, 01:59 PM
  2. How to reuse the replace function for many files ?
    By faezshingeri in forum C Programming
    Replies: 5
    Last Post: 02-14-2012, 06:12 AM
  3. Replies: 3
    Last Post: 01-15-2011, 01:47 PM
  4. Exception handling in a large project
    By EVOEx in forum C++ Programming
    Replies: 7
    Last Post: 01-25-2009, 07:33 AM
  5. Large project
    By cliio in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 10:07 AM

Tags for this Thread