标签: multiple-definition-error

如何链接来自同时具有"主"的.cpp源文件的函数?

我想使用.cpp源文件中的一些函数,该文件在我的.cpp源文件中有一个main函数.(我正在用make和gcc构建.)

这是我的Makefile中的规则:

$(CXX) $(CXXFLAGS) $(INCLUDES) $(SRCS) $(LIBS) -o $@
Run Code Online (Sandbox Code Playgroud)

这是输出(改变一些名称以防止分心):

$ make foo
g++ -g -Wall -m32 -Ilinux/include foo.cpp bar.cpp -o foo
/tmp/ccJvCgT3.o: In function `main':
/home/dspitzer/stuff/bar.cpp:758: multiple definition of `main'
/tmp/ccUBab2r.o:/home/dspitzer/stuff/foo.cpp:68: first defined here
collect2: ld returned 1 exit status
make: *** [foo] Error 1
Run Code Online (Sandbox Code Playgroud)

我如何向gcc表明我想使用foo.cpp中的main?

更新:我应该补充一点,"bar.cpp"是"别人的"代码,并且有自己的主要原因.(听起来我应该与其他人合作让他将共享功能拆分成一个单独的文件.)

c++ linker gcc program-entry-point multiple-definition-error

2
推荐指数
1
解决办法
191
查看次数

在全局范围内声明它时发生命名空间错误

我有3个文件Test.h,Test.cpp和main.cpp

Test.h

#ifndef Test_H
#define Test_H
 namespace v
{
    int g = 9;;
    }
class namespce
{
public:
    namespce(void);
public:
    ~namespce(void);
};
#endif
Run Code Online (Sandbox Code Playgroud)

TEST.CPP

   #include "Test.h"


namespce::namespce(void)
{
}

namespce::~namespce(void)
{
}
Run Code Online (Sandbox Code Playgroud)

Main.cpp的

#include <iostream>
using namespace std;
#include "Test.h"
//#include "namespce.h"


int main ()
{

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

在建设过程中出现以下错误..

1>namespce.obj : error LNK2005: "int v::g" (?g@v@@3HA) already defined in main.obj
1>C:\Users\E543925\Documents\Visual Studio 2005\Projects\viku\Debug\viku.exe : fatal error LNK1169: one or more multiply defined symbols found
Run Code Online (Sandbox Code Playgroud)

请尽快帮助..

c++ multiple-definition-error

2
推荐指数
1
解决办法
889
查看次数

来自C++的C++模板


我试图从C中的C++标准库重新创建一些类.例如,std :: pair类.
为了模拟模板,我当然使用了宏.以下是它的外观示例:

#define _TEMPLATE_PAIR_STRUCT(T1, T2, TNAME, STRNAME) \
typedef struct {                                  \
    T1* first;                                    \
    T2* second;                                   \
} STRNAME;

#define _TEMPLATE_PAIR_NEW(T1, T2, TNAME, STRNAME) \
    STRNAME* TNAME##_new()                         \
    {                                              \
        STRNAME *new = malloc(sizeof( STRNAME ));  \
        new->first = malloc(sizeof(T1));           \
        new->second = malloc(sizeof(T2));          \
        return new;                                \
    }
Run Code Online (Sandbox Code Playgroud)

如果我试图在多个源文件中使用此结构,我必须多次生成代码.显然,这会导致错误.

有没有办法解决这个问题,所以我可以在C中使用这些"模板"?

c c++ macros templates multiple-definition-error

2
推荐指数
1
解决办法
244
查看次数

多重定义 - 为什么我不能在标题中定义它们

我知道......以某种方式的基础知识,但我不明白。我读到我可以将我的函数定义放在我的头文件中,然后包含它。到目前为止我从未这样做过,因为我喜欢我在 .cpp 文件中的定义与它在头文件中的声明分开。

现在我只有两个函数,我不想要这些定义的额外文件。

头文件.h

const QString reviewToString(const int); //Declaration - do I even need it now?
const QString statusToString(const int); //Declaration - do I even need it now?

const QString reviewToString(const int r) //Definition
{
    switch(r)
    {
    case 0:
        return "Excellent";
    case 1:
        return "Great";
    case 2:
        return "Okay";
    case 3:
        return "Poor";
    case 4:
        return "Terrible";
    default:
        return "Unknown";
    }
}

const QString statusToString(const int s) //Definition
{
    switch(s)
    {
    case 0:
        return "Watched";
    case 1:
        return "Bought"; …
Run Code Online (Sandbox Code Playgroud)

c++ linker-errors header-files multiple-definition-error

2
推荐指数
1
解决办法
466
查看次数

C++中头文件中的内联函数

为什么在头文件中定义类的功能是不好的做法?

让我说我有一个头文件,我在类定义本身定义类的功能,如,

headerfile.hpp

#ifndef _HEADER_FILE_
#define _HEADER_FILE_

class node{
int i;

public:
int nextn(){
......
return i;
}
}

#endif //_HEADER_FILE_
Run Code Online (Sandbox Code Playgroud)

因此在类中定义函数就像这样使函数"内联".所以如果我们在两个.cpp文件中包含这个头文件,它会导致"多重定义错误"吗?定义像这样的函数是不好的做法这在课程定义中?

c++ header function multiple-definition-error

2
推荐指数
1
解决办法
3379
查看次数

由于在多个编译单元的情况下包含相同的头文件,因此避免多重定义

我有一个经常使用的功能f().我想要f()在头文件中util.h,这样我就可以f()轻松使用而无需任何额外的编译:

user1.c:

#include "util.h"
int main(){
    f();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

util.h:

void f(){
    // do some job
}
Run Code Online (Sandbox Code Playgroud)

汇编user1.c:

gcc -o user1 user1.c
Run Code Online (Sandbox Code Playgroud)

当有两个编译单元unit2.o和时,会出现问题unit3.o.他们的源代码如下:

user2.c:

#include "util.h"

void user2_function(){
     f();
     // do other jobs
}
Run Code Online (Sandbox Code Playgroud)

user3.c:

#include "util.h"
extern void user2_function();
int main(){
    f();
    user2_function();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我得到multiple definition of f的错误,当我尝试如下编译这些源代码:

gcc -c user2.c
gcc -c user3.c
gcc -o …
Run Code Online (Sandbox Code Playgroud)

c header-files multiple-definition-error

2
推荐指数
1
解决办法
5170
查看次数

C多重定义/首先在此定义错误

我正在尝试写一个书店程序,我在我的函数实现的源代码文件中收到一条错误"多重定义".

这是我的Book.c文件:

#include "Book.h"

void scanBook(book_t* bk) //error here
{
    //implementation here
}
Run Code Online (Sandbox Code Playgroud)

这是我的Book.h文件:

#pragma once
#include <stdio.h>

typedef char* str;    
typedef enum Genre {Education, Business, Novel} genre_t;

typedef struct Book{
    str ISBN;
    str title;
    str author;
    float price;
    int quantity;
    genre_t genre;
} book_t;

void scanBook(book_t* bk);
Run Code Online (Sandbox Code Playgroud)

这是我的main.c文件:

#include "Book.h"
#include "Book.c"

int main()
{
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

该错误发生在Book.c中的scanBook函数但我不知道为什么,因为我包括头文件以及#pragma一次,并在头文件中我声明了该函数.它说'scanBook'和obj\Debug\Book.o ....的多个定义首先在这里定义.

任何帮助或澄清将不胜感激!

c multiple-definition-error modularization

2
推荐指数
1
解决办法
9973
查看次数

多重定义错误c ++

我的'Headers.h'文件包含基本的c ++标题

#include <iostream>
#include <cstring>
// and many header files.
Run Code Online (Sandbox Code Playgroud)

为文件存在检查写了一个函数定义并将其保存在'common_utility.h'中 - ifFileExist()

common_utility.h

bool ifFileExist()
{
   // ... My code
}
Run Code Online (Sandbox Code Playgroud)

为A类classA.h写了代码

class A
{
// Contains class A Declarations.

};
Run Code Online (Sandbox Code Playgroud)

classA.cpp

// Contains
#include "Headers.h"
#include "common_utility.h"
#include "classA.h"

// class A Method definition
Run Code Online (Sandbox Code Playgroud)

为B类写了代码我在B组使用A类.

classB.h

 class B
    {
// Contains class A Declarations.
}
Run Code Online (Sandbox Code Playgroud)

classB.cpp

// Contains
#include "Headers.h"
#include "common_utility.h"
#include "classA.h"
#include "classB.h"

// class B Method definition
// calling the function ifFileExist() in …
Run Code Online (Sandbox Code Playgroud)

c++ header-files multiple-definition-error

2
推荐指数
1
解决办法
1万
查看次数

结构的多重定义错误,但我在任何地方都没有看到错误(C++)

制作了一个结构,编译了我的程序,工作完美,没有改变任何东西,关闭虚拟机,重新启动虚拟机和VSC,然后再次在控制台中进行制作,并出现此错误

1 warning generated.
linking build/main.o
build/Game.o:(.bss+0x60): multiple definition of `scoreArray'
build/main.o:(.bss+0x10): first defined here
clang-5.0: error: linker command failed with exit code 1 (use -v to see 
invocation)
Makefile:33: recipe for target 'build/2048' failed
make: *** [build/2048] Error 1
Run Code Online (Sandbox Code Playgroud)

这是我的代码,我看不到任何重新定义它的东西,请有人帮我找到它,这是头文件和它使用的源文件

主程序

/**
 * File: main.cpp
 * Author: not lettin u know :)
 * Date: 20-11-2019
 * Desc:
 * Copyright: University of West of England 2017
 */
#include <stdio.h>
#include <cstdio>
#include <stdlib.h>
// include the UFCFGL301's standard library
#include …
Run Code Online (Sandbox Code Playgroud)

c++ multiple-definition-error

2
推荐指数
1
解决办法
1813
查看次数

类方法的多重定义

我有文件long_arithm.cpp:

#ifndef LONG_ARITHM.CPP
#define LONG_ARITHM.CPP

#include <iostream>
#include <list>

namespace long_arithm {

    typedef signed char schar;
    enum { error_char = 127 };

    class longint {
    public:
        longint() : minusSign(0), array() { }
        longint(int num) { fromInt(num); }
        longint(std::string str) { fromString(str); }
        longint(const longint& other) : minusSign(other.minusSign), array(other.array) { }

        void fromInt(int num);
        void fromString(std::string str);

    protected:
        schar digtochar(schar num);
        schar chartodig(schar ch);

        inline bool isDigit(schar ch) { /* code */ }
        inline bool isSpaceChar(schar ch) { /* code */ …
Run Code Online (Sandbox Code Playgroud)

c++ compiler-errors compilation build multiple-definition-error

1
推荐指数
1
解决办法
5021
查看次数