所以我的程序运行正常.编译,链接,运行,工作.然后,我决定在我的一个文件中添加一个简单的函数,如下所示:
#ifndef UTILITY_HPP
#define UTILITY_HPP
/* #includes here. There's no circular include, I've checked. */
namespace yarl
{
namespace utility
{
(several function declarations and definitions)
bool isVowel(const char c)
{
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
return true;
else return false;
}
}
}
#endif
Run Code Online (Sandbox Code Playgroud)
该函数定义是我对代码所做的唯一更改.其他一切与现在完全一样.什么都没有叫它.我编译,它无法链接,g ++为#includes这个文件的每个文件提供了这些错误之一:
./obj/Feature.o: In function `yarl::utility::isVowel(char)':
/home/max/Desktop/Development/Yarl Backup/yarl v0.27/src/Utility.hpp:130: multiple definition of `yarl::utility::isVowel(char)'
./obj/Events.o:/home/max/Desktop/Development/Yarl Backup/yarl v0.27/src /Utility.hpp:130: first defined here
./obj/GameData.o: …Run Code Online (Sandbox Code Playgroud) 除了将hello()函数移动到另一个源(.cpp)文件或重命名该函数.有没有其他方法可以避免链接错误?
staticLibA.h
#ifndef _STATIC_LIBA_HEADER
#define _STATIC_LIBA_HEADER
int hello(void);
int hello_staticLibA_only(void);
#endif
Run Code Online (Sandbox Code Playgroud)
staticLibA.cpp
#include "staticLibA.h"
int hello(void)
{
printf("\nI'm in staticLibA\n");
return 0;
}
int hello_staticLibA_only(void)
{
printf("\nstaticLibA: hello_staticLibA_only\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
g++ -c -Wall -fPIC -m32 -o staticLibA.o staticLibA.cpp
ar -cvq ../libstaticLibA.a staticLibA.o
a - staticLibA.o
Run Code Online (Sandbox Code Playgroud)
staticLibB.h
#ifndef _STATIC_LIBB_HEADER
#define _STATIC_LIBB_HEADER
int hello(void);
int hello_staticLibB_only(void);
#endif
Run Code Online (Sandbox Code Playgroud)
staticLibB.cpp
#include "staticLibB.h"
int hello(void)
{
printf("\nI'm in staticLibB\n");
return 0;
}
int hello_staticLibB_only(void)
{
printf("\nstaticLibB: hello_staticLibB_only\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出: …
我试图静态链接libDuma时遇到以下错误,你能告诉我如何让g ++从libDuma中使用malloc吗?
sunny@sunny-laptop:~/CodeTest$ g++ ./testDuma.cpp -g -o testDuma -static -lduma -pthread
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libc.a(malloc.o): In function `free':
(.text+0x4b00): multiple definition of `free'
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libduma.a(duma.o):(.text+0x25f0): first defined here
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libc.a(malloc.o): In function `malloc':
(.text+0x4bc0): multiple definition of `malloc'
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libduma.a(duma.o):(.text+0x2730): first defined here
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libc.a(malloc.o): In function `realloc':
(.text+0x5950): multiple definition of `realloc'
/usr/lib/gcc/i686-linux-gnu/4.4.5/../../../../lib/libduma.a(duma.o):(.text+0x23d0): first defined here
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud) 我有一个名为FindAndReplaceBar的类,其实现如下:
#include "FindAndReplaceBar.h"
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QTextDocument>
#include <QLineEdit>
FindAndReplaceBar::FindAndReplaceBar(QObject *parent) :
QToolBar(NULL)
{
lblFind = new QLabel("Find: ",this);
lblReplace = new QLabel("Replace",this);
ledtFind = new QLineEdit(this);
ledtReplace = new QLineEdit(this);
QPixmap next(":/res/resources/next.gif");
QPixmap previous(":/res/resources/previous.gif");
QPixmap close(":/res/resources/close_icon.gif");
btnFindNext = new QPushButton(QIcon(next),"",this);
btnFindPrevious = new QPushButton(QIcon(previous),"",this);
btnClose = new QPushButton(QIcon(close),"",this);
btnReplace = new QPushButton("Replace",this);
btnReplaceAll = new QPushButton("Replace All",this);
btnFindNext->setFlat(true);
btnFindPrevious->setFlat(true);
btnClose->setFlat(true);
btnReplace->setFlat(true);
btnReplaceAll->setFlat(true);
lytFindAndReplaceBar = new QGridLayout(this);
lytFindAndReplaceBar->addWidget(lblFind,0,0,1,1);
lytFindAndReplaceBar->addWidget(ledtFind,0,1,1,2);
lytFindAndReplaceBar->addWidget(btnFindPrevious,0,3,1,1);
lytFindAndReplaceBar->addWidget(btnFindNext,0,4,1,1);
lytFindAndReplaceBar->addWidget(lblReplace,0,5,1,1);
lytFindAndReplaceBar->addWidget(ledtReplace,0,6,1,2);
lytFindAndReplaceBar->addWidget(btnReplace,0,8,1,1);
lytFindAndReplaceBar->addWidget(btnReplaceAll,0,9,1,1);
this->setLayout(lytFindAndReplaceBar); …Run Code Online (Sandbox Code Playgroud) 我正在编写一些代码,我需要一个类变量,它是一个静态int数组.我明白我可以在头文件中用这样的东西做这个,啊:
#ifndef A_H_
#define A_H_
class A
{
public:
static const int a[];
};
const int A::a[] = {1,2};
#endif
Run Code Online (Sandbox Code Playgroud)
如果我将此标题只包含在另一个文件中,如下所示,main.cpp:
#include "A.h"
#include <iostream>
using namespace std;
int main()
{
A myA;
cout << "0: " << myA.a[0] << endl;
cout << "1: " << myA.a[1] << endl;
}
Run Code Online (Sandbox Code Playgroud)
但是假设我需要我的A类有点复杂,我想要一个A.cpp文件.我将保持我的main.cpp文件相同,但然后更改如下(我刚刚添加了一个函数,printA):
#ifndef A_H_
#define A_H_
class A
{
public:
void printA() const;
static const int a[];
};
const int A::a[] = {1,2};
#endif
Run Code Online (Sandbox Code Playgroud)
然后在文件A.cpp中:
#include "A.h"
#include <iostream>
using namespace …Run Code Online (Sandbox Code Playgroud) Recently I learned the beautiful language D, which is so more plastic and helps yourself writing stable fast programs. But its not popular... because few code written on D and so more on C and C++. Therefore after I read the book of Andrei Alexanderscu where author very superficially described question about linking of D library to C++ code, I tried learn it myself and written some code on D where defined function that returns an instance of CompleteAutomata class …
我一直在尝试使用extern来使用先前定义的变量.
我以前没有使用extern,现在我需要使用它来定义变量一次并在多个文件中使用它们
我已经为这个问题编写了最小化代码版本.我有四个文件
lib.h
#ifndef LIB_H
#define LIB_H
#include <iostream>
namespace lib {
extern bool initialized;
bool initialized = false;
static void isInit(char* parent) {
std::cout << "Library for [" << parent << "] initialized? " << (::lib::initialized ? "yes" : "no") << "\n";
}
} // namespace lib
#endif
Run Code Online (Sandbox Code Playgroud)
vehicle.h
#ifndef _VEHICLE_H
#define _VEHICLE_H
#include <string>
class Vehicle {
public:
Vehicle(const std::string& manufacturer,
const std::string& model,
int year);
std::string manufacturer;
std::string model;
int year;
};
#endif
Run Code Online (Sandbox Code Playgroud)
以下是vehicle.h文件的实现,名为vehicle.cpp
#include "vehicle.h" …Run Code Online (Sandbox Code Playgroud) 创建以下类后,编译失败,出现许多"重复符号"错误.实际错误不是很具描述性:
"复制符号__Zeq .... in:/Users/myusername/Library/Developer/Xcode/DerivedData/MyProject-asdfasfasdf..../Build/Intermediates/MyProject.build/Debug-iphonesimulator/MyTarget.build/Objects-normal /i386/MyClass.o"
上面相同的消息出现在许多不同的类中,并出现在编译结束时,所以我不知道问题是什么.
我检查了以下内容:
问题是什么?
#ifndef Listening_hpp
#define Listening_hpp
#include <stdio.h>
#include "EAction.hpp"
class Listening{
private:
int _emitterId;
int _listenerId;
std::string _eventName;
EAction* _actionToTake; //not owned.
protected:
public:
Listening(int emitterId,int listenerId,std::string eventName,EAction* actionToTake) : _emitterId(emitterId),_listenerId(listenerId),_eventName(eventName){
_actionToTake = actionToTake;
}
int getEmitterId()const{
return _emitterId;
}
int getListenerId()const{
return _listenerId;
}
std::string getEventName()const{
return _eventName;
}
EAction* getAction()const{
return _actionToTake;
}
};
bool operator==(const Listening &first,const Listening &other)
{
bool result = false;
if(first.getEmitterId() == other.getEmitterId()){
if(first.getListenerId() …Run Code Online (Sandbox Code Playgroud) 我有 2 个静态库,它们的编译方式不同。在这一点上,我不明白其中的区别。
我只是想了解,对于一个库中的相同符号,该符号的地址是 0。而在另一个库中则不同于零。这是什么意思?
$ nm works/libdriver.a | grep mbedtls_cipher_setup
0000000000000487 T tls_cipher_setup
$ nm not_works/libdriver.a | grep mbedtls_cipher_setup
0000000000000000 T tls_cipher_setup
Run Code Online (Sandbox Code Playgroud)
当链接器使用这些库进行链接时会有什么区别?
就我而言,如果我使用works/libdriver.a库,链接就会成功通过。但是,当我使用 not_works/libdriver.a 库时,相同的链接会生成多重定义错误,而符号 tls_cipher_setup 是生成此错误的众多错误之一。
我遇到过这样的情况:librdkafka和Pulsar C++ 客户端的链接顺序不同确实很重要,因为它们都包含各自的 LZ4 压缩版本。由于 LZ4 函数的多个定义(librdkafka 和 Pulsar 的这些函数的名称相同),链接失败。我检查了静态库,但我找不到任何可疑的东西,为什么它按一个顺序工作而按另一个顺序却不工作。因为很难为这些大库提供最小的工作示例,所以我尝试重现相同的情况,并且我能够做到这一点。我创建了一个小项目,其中链接顺序很重要。
libA.hpp:
#pragma once
void NotClashingFunctionA();
void ClashingFunction();
Run Code Online (Sandbox Code Playgroud)
libA.cpp:
#include "libB.hpp"
#include <iostream>
void NotClashingFunctionA() {
std::cout << "Not clashing function A\n";
}
void ClashingFunction() {
std::cout << "Clashing function A\n";
}
Run Code Online (Sandbox Code Playgroud)
libB.hpp:
#pragma once
void NotClashingFunctionB();
Run Code Online (Sandbox Code Playgroud)
libB.cpp:
#include "libB.hpp"
#include <iostream>
void NotClashingFunctionB() {
std::cout << "Not clashing function B\n";
}
Run Code Online (Sandbox Code Playgroud)
libBSub.hpp:
#pragma once
void ClashingFunction();
Run Code Online (Sandbox Code Playgroud)
libBSub.cpp:
#include "libBSub.hpp"
#include <iostream>
void ClashingFunction() { …Run Code Online (Sandbox Code Playgroud)