c++cvisual-studiopch

First Time Using PCH, getting Linker Tool Error


I'm a pretty novice programmer, just learning a little bit of c, but I always did it on Linux with gcc and Vim but decided to try using visual studio and I'm getting LNK2005 and LNK1169 errors, I've tried looking up the errors and how to fix them and properly use PCH because I think it would be useful to learn even if my programs are too small to make use of it.

From my understanding I need to #include "stdafx.h" at the top of my source file (called 'helloworld.c') I haven't touched 'stdafx.c' from the default that came when I create the project, I created a header file called 'bitwise.h' and it has one function in it called 'int bw()' I then have 'stdafx.h' and all I added was #include "bitwise.h" In my headerbitwise.h ive tried to include #include "stdafx.h" #include "stdafx.c" #include <stdio.h> and even not including anything. all of these break my program. The only way I can get it to compile is if i comment out//bw(); then my program compiles just fine.

here are the files that I think may be the culprit:

helloworld.c

#include "stdafx.h"

int main()
{

    printf("\tHello World!\n");
    getchar();
    bw(); //If this line is commented out everything works just Honky-Dory
    getchar();
    return 0;
}

bitwise.h

#include "stdafx.h" //I've tried lots of diffrent lines here, nothing works

int bw()
{
        int a = 1;
        int x;

        for (x = 0; x < 7; x++)
        {
            printf("\nNumber is Shifted By %i Bits: %i", x, a << x);
        }
        getchar();

        return 0;
}

stdafx.c

// stdafx.cpp : source file that includes just the standard includes
// $safeprojectname$.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file

stdafx.h

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"
#include "bitwise.h"
#include <stdio.h>
#include <tchar.h>



// TODO: reference additional headers your program requires here

Solution

  • This is not much about PCH. You have mixed up header (.h) and implementation (.c) files. What you need to do is split implementation and declaration. You should do following:

    1. rename your bitwise.h to bitwise.c since this is your implementation file, not a header!

    2. create a new file bitwise.h and put only declaration there, it should look like this:

      #pragma once
      
      int bw();
      

    Your project should be able to compile after that.

    Please also note PCH file should contain includes that are infrequently changed, which is probably not your case since you are including also bitwise.h. You might want to remove this include from stdafx.h and include it into your helloworld.c.

    And just a side note, during learning C do not think of including .c file via #include! If it fixes some of your compilation errors, your project design is probably very wrong.