Anne Quinn
Anne Quinn

Reputation: 13040

C++ Linker Trouble, Unresolved External Symbol with my own code

I've been having a rather frustrating linker issue that doesn't make much sense to me. I have the 4 following files:

// CompileFunctions.h
#pragma once
#include "FileListing.h"
int compileSheet(FileListing & file);
int compileVerse(FileListing & file);

// CompileSheet.cpp
#include "CompileFunctions.h"
int compileSheet(FileListing & file){
    ...
}

// CompileVerse.cpp
#include "CompileFunctions.h"
int compileVerse(FileListing & file){
    ...
}

// main.cpp
#include "CompileFunctions.h"
int main(){
    ...
    compileSheet(file);
    compileVerse(file);
}

This generates the following linker error:

main.obj : error LNK2001: unresolved external symbol "int __cdecl compileVerse(struct FileListing &)" (?compileVerse@@AAUFileListing@@@Z)

I've tried adding extern to the declarations in the header, checked to make sure both source files were in the same directory and compiling, and cleaned the build. But nothing seems to help. Given how an identical function is linking fine while the other is not is baffling me, I'm not sure what's causing this.

Upvotes: 0

Views: 141

Answers (1)

Anne Quinn
Anne Quinn

Reputation: 13040

picture of config menu

Following one of the comments, it seemed like VerseCompiler.cpp was acting weird. I checked it's property page to see if it was being excluded from the build and found another culprit:

Despite the file's name, it was being treated as a header file by Visual Studio. I'm guessing I must've mistakenly created it as a .cpp file and renamed it without realizing this didn't change it's behaviour. Setting this to C/C++ Compiler fixed my linker issue.

Upvotes: 1

Related Questions