Thread: confusion on linking files

  1. #1
    Registered User
    Join Date
    Jan 2024
    Posts
    23

    confusion on linking files

    Hello,

    The following code just implements a dynamic array that is grown to double in size once its full. I do not understand why the reference cannot be resolved by the linker:

    I have a file "memory.h" and "memory.c":

    Code:
    // memory.h
    #ifndef clox_memory_h
    #define clox_memory_h
    
    
    #include "common.h"
    
    
    #define GROW_CAPACITY(capacity) ((capacity) < 8 ? 8 : (capacity) * 2)
    
    
    #define GROW_ARRAY(type, pointer, oldCount, newCount) \
        (type*)reallocate(pointer, sizeof(type) * (oldCount), \
            sizeof(type) * (newCount))
    
    
    void* reallocate(void* pointer, size_t oldSize, size_t newSize);
    
    
    #endif
    Code:
    // memory.c
    #include <stdlib.h>
    
    #include "memory.h"
    
    
    void* reallocate(void* pointer, size_t oldSize, size_t newSize) {
      if (newSize == 0) {
        free(pointer);
        return NULL;
      }
    
    
      void* result = realloc(pointer, newSize);
      if (result == NULL){
        exit(1);
      }
      return result;
    }
    and the file that includes "memory.h" - "segment.c":

    Code:
    // segment.c where the linker cannot resolve reallocate() 
    // in append_segment()
    #include "segment.h"
    #include <errno.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "memory.h"
    
    
    void init_segment(code_segment* segment)...
    
    
    // the error originates here...
    void append_segment(code_segment* segment, uint8_t byte){
        if (segment->count + 1 > segment->capacity){
            segment->code = GROW_ARRAY(uint8_t, segment->code,
                segment->capacity, GROW_CAPACITY(segment>capacity));
        }
        segment->code[segment->count] = byte;
        segment->count++;
        
    }
    
    
    void free_segment(code_segment* segment)...
    this is the makefile and I dont understand why the linker cannot resolve
    the reference to "reallocate()" in "append_segment()".
    Code:
    all: main test_segment segment.o
    
    
    ... (more targets)
    
    
    segment.o: segment.c memory.c
        gcc -Wall -c segment.c -o segment.o
    
    
    memory.o: memory.c
        gcc -Wall -c memory.c -o memory.o
    
    
    ... (more targets)

  2. #2
    Registered User
    Join Date
    Jan 2024
    Posts
    23
    please ignore this, I refactored my code.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Code:
    # Makefile
    
    CFLAGS=-Wall
    main: segment.o memory.o (more objects)
    
    # Implicit rules
    segment.o: segment.c segment.h memory.h
    memory.o: memory.c memory.h

  4. #4
    Registered User
    Join Date
    Jan 2024
    Posts
    23
    thanks! I have written a file with tests, "test_segment.c/h" and updated I have updated the Makefile. Every target " *.o" gets all its included headers as additional dependencies but my problem remains.
    Is it correct to use a header file as requirement on multiple targets?
    Or does the linker link methods multiple times and there is a name clash?

    Thank you again for taking the time!

    Code:
    # Makefile
     
    CFLAGS=-Wall
    all: main test_segment
    
    
    test_segment: test_segment.o debug.o segment.o
    main: segment.o memory.o debug.o
     
    # Implicit rules
    test_segment.o: test_segment.c test_segment.h segment.h debug.h memory.h
    segment.o: segment.c segment.h memory.h
    memory.o: memory.c memory.h
    debug.o: debug.c debug.h
    Code:
    //test_segment.h
    
    #include "segment.h"
    
    
    code_segment* build_test_segment();
    void test_init_segment();
    void test_append_segment();
    void test_free_segment();
    void fill_segment(uint8_t values[]);
    void test_debug_file();
    Code:
    // test_segment.c
    #include "test_segment.h"
    #include "common.h"
    #include "segment.h"
    #include <stdio.h>
    #include <assert.h>
    #include <stdlib.h>
    #include "debug.h"
    
    
    implementation of the methods....
    
    
    
    
    int main(int argc, char* argv[]){
    
    
        main code.....
    
    
        return 0;
    }
    and the "memory.c/h" file looks like this:
    Code:
    #ifndef memory_h
    #define memory_h
    
    
    #include "common.h"
    
    
    #define GROW_CAPACITY(capacity) ((capacity) < 8 ? 8 : (capacity) * 2)
    
    
    #define GROW_ARRAY(type, pointer, oldCount, newCount) \
        (type*)reallocate(pointer, sizeof(type) * (oldCount), \
            sizeof(type) * (newCount))
    
    
    void* reallocate(void* pointer, size_t oldSize, size_t newSize);
    
    
    #endif
    Code:
    #include <stdlib.h>
    
    
    #include "memory.h"
    
    
    void* reallocate(void* pointer, size_t oldSize, size_t newSize) {
      if (newSize == 0) {
        free(pointer);
        return NULL;
      }
    
    
      void* result = realloc(pointer, newSize);
      if (result == NULL){
        exit(1);
      }
      return result;
    }

  5. #5
    Registered User
    Join Date
    Jan 2024
    Posts
    23
    problem solved I did not include "memory.o"......

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Linking Files
    By deltagods in forum C Programming
    Replies: 9
    Last Post: 10-05-2009, 03:59 AM
  2. confusion in linking process
    By surrounded in forum C Programming
    Replies: 2
    Last Post: 03-03-2009, 08:24 AM
  3. Files declaration confusion
    By guillermoh in forum C Programming
    Replies: 1
    Last Post: 02-04-2008, 10:26 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Linking files
    By Panopticon in forum C++ Programming
    Replies: 1
    Last Post: 01-12-2003, 06:26 PM

Tags for this Thread