1

I am trying to import a header file from a project to a .c file in another project. The idea is to have a generic project of headers and functions which can be used by multiple project in the same solution.

These are the header file and it's .c file in their own project:

Util_library.h:

#pragma once
#include <string.h>

void reverse(char *c);

Library.c:

#include "Util_Library.h"

void reverse(char *c) {   //reverse function to be called by main functions
    size_t len = strlen(c) - 1, i, k = len - 1; //purpose of function is to
    char tmp;                                   //reverse a given string
    for (i = 0; i < len / 2; i++) {
        tmp = c[k];
        c[k] = c[i];
        c[i] = tmp;
        k--;
    }
}

This is the main function (in another separate project [but in the same solution]) which by #includeing the header file Util_Library.h, it can make use of the function reverse():

#include <stdio.h>
#include <stdlib.h> //malloc library
#include "C:\Users\...\Library\Util_Library.h" //not real path

    void main() {
    char ch[100];
    printf("Enter a string of characters:");
    fgets(ch, sizeof(ch), stdin);
    printf("%s", ch);
    reverse(ch);
    printf("%s", ch);
    int chk = getchar();
} 

Even though I have no syntax errors (according to the compiler that is), when I try to run the program I get these 2 errors:

Error LNK2019 unresolved external symbol "void __cdecl reverse(char *)" (?reverse@@YAXPEAD@Z) referenced in function main Assignment 1D C:\Users...\Assignment_1D\Assignment_1D.obj 1

Error LNK1120 1 unresolved externals Assignment 1D C:\Users\mmusc\Dropbox\Development\C(++) - Visual Studio\Assignments\x64\Debug\Assignment 1D.exe 1

Thanks for your help!

edit I have included the actual .c file which contains the function reverse(), however is there anyway to import just a header file instead of the whole .c file which might even contain functions you would not need in the specific main function edit

Logan
  • 267
  • 3
  • 8
  • 18
  • 1
    Possible duplicate of [LNK2019 problem](http://stackoverflow.com/questions/1763480/lnk2019-problem) – Mgetz Nov 26 '15 at 14:31

2 Answers2

4

It is not only the matter of including the header files: you must also include the .c files in your project.

You're receiving a linker error because, when it is the time to link each call with the function itself, the function is not there, since you have not compiled the corresponding .c file.

Go to your project options and include that .c file in your project. Recompile and you'll have your program running.

Hope this helps.

BTW, this isn't specific to Visual Studio, it will happen in any IDE you apply this strategy in.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
  • Thanks, worked like a charm. However I was wondering if it is possible to just including the header files, since the .c file might contain functions I would not need, which would be unnecessary to import in my code? – Logan Nov 26 '15 at 14:39
  • Yes, you can build the utility functions into a library and include the library path and name of the library in the settings for your main project. You should also set the include path for the library header files in the settings for the main project so you don't have to use the full path-name of the header file in your main .c file. – Ian Abbott Nov 26 '15 at 15:35
  • Remember that, if you create a library, that that library will be specific to your OS and to your compiler. It should be, however, the desired option. – Baltasarq Nov 27 '15 at 09:13
1

Including the header makes the declaration of reverse available, but not the implementation.

Unfortunately, the C++ build model is quite archaic, here is a decent introduction

You can add the .c file to your project

Alternatively, you can compile the reverse .c / .h file (together with other files) to a static library, and add the static libary to your main project. Unfortunately, some compile options between the library must match (e.g. how it links to the C++ runtime). Furthermore, you may want to build separate debug and release libraries. Even more unfortunately, these libraries are not portable between compilers, often not even between different versions of the same compiler

Community
  • 1
  • 1
peterchen
  • 40,917
  • 20
  • 104
  • 186