简体   繁体   中英

Visual Studio 2015 C++ - external header files

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 #include ing 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

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.

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 (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM