Fatal error LNK1169: one or more multiply defined symbols found
Solution 1
I stopped reading after # include "linklist.cpp"
. Don't include implementation files in other implementation files. (unless you're doing bulk-builds, which I doubt). Separate declarations in headers and include those, and keep the definitions in the implementation files.
Solution 2
You have two ways to solve your problem:
First is given in answer of Luchian Grigore. Create separate header and include it in main file.
Second one is exclude file linklist.cpp from build using project options. In other way this file will be build twice: during his own build and during main file build.
However, second way is not good programming style. It is better to create header file.
OOkhan
Updated on June 04, 2022Comments
-
OOkhan about 2 years
I am getting this error:
fatal error LNK1169: one or more multiply defined symbols found
Below are two files containing the code. In file 1, I have the
main()
function and I am calling the functions which are written in the second file namedlinklist.cpp
. Thanks for helping in advance.File 1 - main.cpp
#include "stdafx.h" # include "linklist.cpp" int main(int argc, _TCHAR* argv[]) { node *link_list2; link_list2 = createList(31); addFront(link_list2,33); printList(link_list2); printf("Hello There Omer Obaid khan\n"); return 0; }
File 2 - linklist.cpp
# include "stdafx.h" # include <stdlib.h> struct node{ node * next; int nodeValue; }; node* initNode(int number); node* createList (int value); void addFront (node *head, int num ); void deleteFront(node*num); void destroyList(node *list); int getValue(node *list); node*createList (int value) /*Creates a Linked-List*/ { node *dummy_node = (node*) malloc(sizeof (node)); dummy_node->next=NULL; dummy_node->nodeValue = value; return dummy_node; } void addFront (node *head, int num ) /*Adds node to the front of Linked-List*/ { node*newNode = initNode(num); newNode->next = NULL; head->next=newNode; newNode->nodeValue=num; } void deleteFront(node*num) /*Deletes the value of the node from the front*/ { node*temp1=num->next; if (temp1== NULL) { printf("List is EMPTY!!!!"); } else { num->next=temp1->next; free(temp1); } } void destroyList(node *list) /*Frees the linked list*/ { node*temp; while (list->next!= NULL) { temp=list; list=temp->next; free(temp); } free(list); } int getValue(node *list) /*Returns the value of the list*/ { return((list->next)->nodeValue); } void printList(node *list) /*Prints the Linked-List*/ { node*currentPosition; for (currentPosition=list->next; currentPosition->next!=NULL; currentPosition=currentPosition->next) { printf("%d \n",currentPosition->nodeValue); } printf("%d \n",currentPosition->nodeValue); } node*initNode(int number) /*Creates a node*/ { node*newNode=(node*) malloc(sizeof (node)); newNode->nodeValue=number; newNode->next=NULL; return(newNode); }
-
OOkhan over 11 yearsyou want me to make .h file and put Signatures of funtion there and then include that header file in main ??
-
SvenS over 11 yearsyes, struct and function prototypes go into a header file, which is included both in main.cpp and linklist.cpp. The #include statement puts the whole content of one file into the file it is used in, so the functions are declared both in linklist.cpp and in main.cpp, that's why you get that error.
-
OOkhan over 11 yearsMr.Luchian Grigore thanks for helping... Can you please tell why the error is coming ? Means what is logic behind? Now i make 1 header file and put prototype and struct there and include that header file on both .cpp files. why i can't just include .cpp file in other one?
-
Luchian Grigore over 11 years@OOkhan you're breaking the one definition rule (look it up). Basically, you can't define symbols multiple times. When you include the
cpp
file, it's contents are pasted in the current translation unit. So you'll define, for example,createList
both inmain.cpp
ANDlinklist.cpp
. Which is illegal. A header files should only contain declarations. What you have in thecpp
file are definitions. -
OOkhan over 11 years@LuchianGrigore what if i put prototypes in main and delete header file and did not include linklist.cpp Should it work?? if No then why not??
-
OOkhan over 11 years@LuchianGrigore But that's not a Good way right ??:) Thanks a Lot SIR
-
Luchian Grigore over 11 years@OOkhan it's not unusual, but having a header is preferred, yes.