0

I'm using a OS X machine and I've written a library in C which I'd be using for future programs.

This is the declaration: (FunzioniListe.h)

#ifndef FUNZIONILISTE_H
#define FUNZIONILISTE_H

#include <stdio.h>
#include <stdlib.h>

struct node {
    int nodeValue;
    char elem;
    struct node *next;
};

struct node *addElemToList(struct node *list, int position, int value, char elemToAdd);

void deleteElem(struct node *list, int value);    
int listLen(struct node *list);    
void printList(struct node *list);

#endif // FUNZIONILISTE_H_INCLUDE

This is the implementation: (FunzioniListe.c)

#include <stdio.h>
#include <stdlib.h>
#include "FunzioniListe.h"

struct node *addElemToList(struct node *list, int position, int value, char elemToAdd) {
    struct node *newNode;
    newNode = malloc(sizeof(struct node));

    if (newNode == NULL) {
        printf("Error: malloc failed");
        exit(EXIT_FAILURE);
    }

    newNode->elem = elemToAdd;
    newNode->nodeValue = value;

    if (position > listLen(list)) {
        while (list->next != NULL) {
            list = list->next;
        }
        list->next = newNode;
    } else {
        int counter = 0;
        while (counter < position) {
            list = list->next;
        }
        newNode->next = list->next;
        list->next = newNode;
    }
    return newNode;
}

void deleteElem(struct node *list, int position) {
    int counter = 0;
    struct node *elemToDelete;
    if (position > 0 && position < listLen(list)) {
        while (counter < (position - 1)) {
            list = list->next;
        }
        elemToDelete = list->next;
        list->next = elemToDelete->next;
        free(elemToDelete);
    } else {
        while (counter < (listLen(list) - 1)) {
            list = list->next;
        }
        elemToDelete = list->next;
        free(elemToDelete);
    }
}

int listLen(struct node *list) {
    int listLen = 0;
    for (; list != NULL; list = list->next) {
        listLen++;
    }
    return listLen;
}

void printList(struct node *list) {
    for (; list != NULL; list = list->next) {
        printf("Node #%d\nElem: %c\n\n", list->nodeValue, list->elem);
    }
}

The problem is that when I try to use this library in another project I get this output:

Undefined symbols for architecture x86_64:
  "_addElemToList", referenced from:
      _main in TestLibreria-ef7c77.o
  "_printList", referenced from:
      _main in TestLibreria-ef7c77.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is a simple program that takes advantages of the library already created:

#include <stdlib.h>
#include <stdio.h>
#include "FunzioniListe.h"

int main() {
    struct node *first = NULL;
    for (int i = 0; i < 7; i++) {
        first = addElemToList(first, 30, i, i + 60);
    }
    printList(first);
}

I don't get what the problem is, both the files are correct but my Mac can't compile this correctly. Any reason why is this happening?

In depth invocation error:

Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.2.0
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple x86_64-apple-macosx10.11.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name TestLibreria.c -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -masm-verbose -munwind-tables -target-cpu core2 -target-linker-version 253.9 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -fdebug-compilation-dir /Users/Matt/CProjects/Lab -ferror-limit 19 -fmessage-length 195 -stack-protector 1 -mstackrealign -fblocks -fobjc-runtime=macosx-10.11.0 -fencode-extended-block-signature -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/1c/6dpsqcrs69s9hgdnj0kkyh780000gn/T/TestLibreria-a8db8d.o -x c TestLibreria.c
clang -cc1 version 7.0.2 based upon LLVM 3.7.0svn default target x86_64-apple-darwin15.2.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/include
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include
 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks (framework directory)
End of search list.
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.11.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -o a.out /var/folders/1c/6dpsqcrs69s9hgdnj0kkyh780000gn/T/TestLibreria-a8db8d.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.2/lib/darwin/libclang_rt.osx.a
Undefined symbols for architecture x86_64:
  "_addElemToList", referenced from:
      _main in TestLibreria-a8db8d.o
  "_printList", referenced from:
      _main in TestLibreria-a8db8d.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
chqrlie
  • 131,814
  • 10
  • 121
  • 189
Mattia Righetti
  • 1,265
  • 1
  • 18
  • 31
  • 6
    please show your compilation line(s). they should be something like `gcc -o TestLibreria-ef7c77 TestLibreria-ef7c77.c FunzioniListe.o`. BTW, no reason to include `stdio.h` or `stdlib.h` in the `FunzioniListe.h` – KevinDTimm Jan 19 '16 at 21:02
  • I've edited the thread, tell me if you need something else – Mattia Righetti Jan 19 '16 at 21:05
  • i guess you forget to include your library in link time as `gcc -lmylib` – milevyo Jan 19 '16 at 21:06
  • You didn't show your compilation lines but I can't see any reference to `FunzioniListe` in the output so I assume you haven't included your object file in the compilation. Add it and your function(s) will be found. – KevinDTimm Jan 19 '16 at 21:08
  • i guess you don't know what include files (*.h) and lib files (*.lib; lib*.a ) are? and how to use them. – milevyo Jan 19 '16 at 21:14
  • I'm trying to learn them. That's why I'm asking, I didn't know it was necessary to link the library – Mattia Righetti Jan 19 '16 at 21:17
  • 1
    Do not insert spaces between a structure pointer and the member name, such as `list -> next`. It is very uncommon to do this and makes the code harder to read. – chqrlie Jan 19 '16 at 22:34
  • a proper way to create a library in clang is `clang -dynamiclib Person.c -o libPerson.dylib` which creates a dynamic library named `libPerson.dylib` from the source file `Person.c`. If you copy the library to /usr/local/lib/. then using that library in any other application means the link step needs the parameters (at the end of the line) `-L/usr/loca/lib -lPerson` The header file should be copied to /usr/local/include/. and the compile step include the parameter: `-I/usr/local/include` – user3629249 Jan 21 '16 at 20:39

2 Answers2

0

You don't link them.

You probably only use gcc -std=c99 main.c (assuming that is the name of the last one). Then the used functions don't get referenced. After running it so on Linux Mint 17.1 Rebecca personally I got

/tmp/ccOdy4br.o: In function main': main.c:(.text+0x32): undefined reference toaddElemToList' main.c:(.text+0x4c): undefined reference to `printList' collect2: error: ld returned 1 exit status

So you should try with linking flag

gcc -std=c99 -c *.c

And then compiling it

gcc -std=c99 *.o

Then I am able to run it and successfully get a segmentation fault.

Note: * is a RegEx for any, it works in the terminal, since Mac is based on Unix.

Dimitar
  • 4,402
  • 4
  • 31
  • 47
-2

Steps to create a C library


  1. Create an interface to the library


    • function prototypes with comments for users of your library functions
    • definitions for types and global variables exported by your library
  2. Create an implementation of the library


    • implementation of every function in your library
  3. Create a library object file


When you have your library.h interface and its library.c implementation, all you have to do is run this command

gcc -o library.o -c library.c
  1. Use the library in another C program


    • Use #include "libraryname.h" in your code

What's missing in my code is step 3.

Now I can compile the C programs that uses my library with the command

gcc cprogram.c library.o
Mattia Righetti
  • 1,265
  • 1
  • 18
  • 31
  • 1
    while functionally correct, step 3 doesn't need `-o library.o`. Also, you're not 'creating a C library' you're 'creating a C object file'. Finally, by convention, you do use `-o` in step 4 to name the executable output file rather than accepting the default `a.out` – KevinDTimm Jan 20 '16 at 14:48