我是 C 套接字编程的初学者。我在书中得到了代码,当我编译时,这些是以下带有未定义引用的错误。请给一个提示来纠正这个问题!谢谢!
代码:
#include <stdio.h>
#include <winsock.h>
#include <winsock2.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define RCVBUFSIZE 32
int main(int argc, char *argv[]){
int sock;
struct sockaddr_in echoServAddr;
unsigned short echoServPort;
char *servIP;
char *echoString;
char echoBuffer[RCVBUFSIZE];
unsigned int echoStringLen;
int bytesRcvd, totalBytesRcvd;
if(argc>3 || argc>4){
printf("Usage: %s <Server IP> <Echo Word> [<Echo Port>\n",argv[0]);
exit(1);
}
servIP=argv[1];
echoString=argv[2];
if(argc==4){
echoServPort=atoi(argv[3]);
}
else{
echoServPort=7;
}
if((sock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0){
printf("socket() failed!");
}
memset(&echoServAddr,0,sizeof(echoServAddr));
echoServAddr.sin_family=AF_INET;
echoServAddr.sin_addr.s_addr=inet_addr(servIP);
echoServAddr.sin_port=htons(echoServPort);
if (connect(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) …Run Code Online (Sandbox Code Playgroud) 由于我的上一个问题因糟糕的代码风格和拼写错误而被关闭,我对其进行了审查并再次寻求帮助。
我正在尝试解析 PE 格式的 .exe 文件。这是我的代码的一部分
#include "PE_resolve.h"
#define SIZEOF_SECTION_HEADER 0x28
/*load filebuffer into Imagebuffer*/
int Read_2_ImageBuffer(void **p_filebuffer, void **p_Imagebuffer,long filesize);
/*helper function*/
inline void * Get_NT_POS(void **p_filebuffer);
inline void * Get_FileHeader_POS(void **p_filebuffer);
inline void * Get_Opt_FileHeader_POS(void **p_filebuffer);
int main(){
return 0;
}
int Read_2_ImageBuffer(void **p_filebuffer, void **p_Imagebuffer,long filesize){
/*allocate memory for imagebuffer*/
void *Opt_PE_Header = Get_Opt_FileHeader_POS(p_filebuffer); //THE ERROR LINE
DWORD SizeOfImage = *(DWORD*) ((BYTE *)Opt_PE_Header + 0x38);
*p_Imagebuffer = malloc(SizeOfImage);
if(*p_Imagebuffer == NULL){
printf("can't allocate enough heap memory\n");
exit(EXIT_FAILURE); …Run Code Online (Sandbox Code Playgroud) 链接器错误:
$ make
g++ -Wall -g main.cpp SDL_Helpers.cpp Game.cpp DrawableObject.cpp `sdl-config --cflags --libs` -lSDL_mixer
/tmp/ccdxzrej.o: In function `Game':
/home/brett/Desktop/SDL/Game.cpp:16: undefined reference to `Player::Player(Game*)'
/home/brett/Desktop/SDL/Game.cpp:16: undefined reference to `Player::Player(Game*)'
collect2: ld returned 1 exit status
make: *** [all] Error 1
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include "Game.h"
int main()
{
Game g;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Game.h:
#ifndef BRETT_GAME_H
#define BRETT_GAME_H
#include <cmath>
#include "SDL.h"
#include "SDL_Helpers.h"
#include <vector>
class DrawableObject;
class Game
{
public:
SDL_Surface * screen;
std::vector<DrawableObject*> sprites;
Game();
};
#endif
Run Code Online (Sandbox Code Playgroud)
Game.cpp:
#include "Game.h" …Run Code Online (Sandbox Code Playgroud) 当我尝试编译以下代码时,我收到此错误:
obj\Debug\main.o||In function `main':|
C:\Users\dmarr\Documents\CSharper\Dallas\CPlusPlus\WHYGODWHYCB\main.cpp|16|undefined reference to `bag::bag()'|
||=== Build finished: 1 errors, 0 warnings ===|
Run Code Online (Sandbox Code Playgroud)
起初,我将bag.h和bag.cpp作为两个单独的文件 - 许多与"Undefined Reference"相关的答案建议将.cpp文件的内容移动到.h文件中 - 所以我做了.但是,编译时我仍然遇到上述错误.
aItem.cpp:
//aItem .cpp implementation file
#include "aItem.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//setting this up default
aItem::aItem()
{
m_itemName = "Default Name";
m_itemType = "Default Type";
m_damage = 9001;
}
void aItem::setName(string name)
{
m_itemName = name;
}
void aItem::setType(string type)
{
m_itemType = type;
}
void aItem::setDamage(int damage)
{
m_damage = damage;
} …Run Code Online (Sandbox Code Playgroud) 可能重复:
C++模板,未定义的引用
我有一个非常简单的程序,由三个文件组成,它们从普通数组构建向量:
//create.hpp
#ifndef CREATE_HPP_
#define CREATE_HPP_
#include <vector>
using namespace std;
template<class T>
vector<T> create_list(T uarray[], size_t size);
#endif /* CREATE_HPP_ */
Run Code Online (Sandbox Code Playgroud)
//create.cpp
#include "create.hpp"
template<class T>
vector<T> create_list(T uarray[], size_t size){
vector<T> ulist(uarray, uarray + size);
return ulist;
}
Run Code Online (Sandbox Code Playgroud)
//main.cpp
#include <vector>
#include <iostream>
#include "create.hpp"
using namespace std;
int main(){
char temp[] = { '/', '>' };
vector<char> uvec = create_list<char>(temp, 2);
vector<char>::iterator iter=uvec.begin();
for(;iter != uvec.end();iter++){
cout<<*iter<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
构建过程如下:
g++ -O0 -g3 …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,动态链接都可以,但是当我尝试使用静态链接进行编译时,我有以下错误.
我的应用程序使用boost线程,asio
错误:
/tmp/ccMj2fHI.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x237): undefined reference to `boost::system::get_system_category()'
test.cpp:(.text+0x243): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x24f): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x25b): undefined reference to `boost::system::get_generic_category()'
test.cpp:(.text+0x267): undefined reference to `boost::system::get_system_category()'
/tmp/ccnCWj1O.o: In function `__static_initialization_and_destruction_0(int, int)':
AccountSe.cpp:(.text+0x507): undefined reference to `boost::system::get_system_category()'
AccountSe.cpp:(.text+0x513): undefined reference to `boost::system::get_generic_category()'
AccountSe.cpp:(.text+0x51f): undefined reference to `boost::system::get_generic_category()'
AccountSe.cpp:(.text+0x52b): undefined reference to `boost::system::get_generic_category()'
AccountSe.cpp:(.text+0x537): undefined reference to `boost::system::get_system_category()'
Run Code Online (Sandbox Code Playgroud)
所有源文件都有类似的错误.
编译命令行:
g ++ -L/usr/lib/-lboost_system -lboost_thread -o newserver -static /usr/lib/libboost_thread.a /usr/lib/libboost_system.a stdafx.cpp test.cpp AccountSe.cpp ... -lpthread -std …
可能重复:类中的
静态成员变量
什么是未定义的引用/未解析的外部符号错误以及如何修复它?
class test_struct {
static int number;
public:
static void set() {
number = 42;
}
};
int main() {
test_struct::set();
}
Run Code Online (Sandbox Code Playgroud)
错误是:
[...]Temp\ccGGxEAz.o:test.cpp:(.text.startup+0xf): undefined reference to `test_struct::number'
collect2.exe: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
#include <iostream>
#include <array>
template <typename Type>
struct Constant
{
constexpr Constant(const Type source) : _data({{source}}) {;}
constexpr Constant(const std::array<Type, 1> source) : _data(source) {;}
constexpr Constant<Type> operator()() const {return _data;}
constexpr operator Type() const {return _data[0];}
const std::array<Type, 1> _data;
static constexpr Constant<Type> pi = 3.1415926535897932384626433832795028841971693993751058209749445L;
};
int main(int argc, char* argv[])
{
std::cout<<Constant<double>::pi()<<std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到一个编译器错误g++4.7.3和g++4.8.0(这是一个未定义的引用pi(抱歉,它是法语)):
/tmp/cctdvPfq.o: dans la fonction « main »:
main.cpp:(.text.startup+0xd): référence indéfinie vers « Constant<double>::pi » …Run Code Online (Sandbox Code Playgroud) 我有libmodbus的源.h和.c文件(http://libmodbus.org/releases/libmodbus-3.0.6.tar.gz).在得知Qt足够聪明以了解b/wc和c ++文件的差异之后,我将它们放入我的Qt项目中.
我在main.cpp中包含modbus.h,我得到大约50个错误(其中大多数是未定义的引用错误).注意我确实让libmodbus在ubuntu中使用代码块工作,并且它正在进行通信.我现在正在Windows中尝试这个,Qt和代码块似乎都对我生气.
这是我的.pro文件.QT + =核心QT + =小部件
QT -= gui
TARGET = testModBus
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
QMAKE_CXXFLAGS += -w
QMAKE_CFLAGS += -w
SOURCES += main.cpp \
modbus.c \
modbus-data.c \
modbus-rtu.c \
modbus-tcp.c
HEADERS += \
config.h \
modbus.h \
modbus-private.h \
modbus-rtu.h \
modbus-rtu-private.h \
modbus-tcp.h \
modbus-tcp-private.h \
modbus-version.h \
zModBus.h \
inttypes.h \
stdint.h
Run Code Online (Sandbox Code Playgroud)
当我尝试编译时,这是我的Qt输出:
18:03:02: Starting: "E:\Qt\Tools\mingw48_32\bin\mingw32-make.exe"
E:\Qt\5.2.1\mingw48_32\bin\qmake.exe -spec win32-g++ -o Makefile ..\testModBus\testModBus.pro
E:/Qt/Tools/mingw48_32/bin/mingw32-make -f Makefile.Release
mingw32-make[1]: …Run Code Online (Sandbox Code Playgroud) 我在Linux下构建了FreeBSD libfortuna库.我做了什么来管理这只是评论#include <malloc_np.h>中的src/px.h和SRC/internal.h,做make,make install并创建在我的系统标准路径库的符号链接.
然后,我为它写了一个小测试程序:
#include <cstdlib>
#include <fortuna.h>
int main () {
int i = 0;
fortuna_get_bytes (4, (unsigned char *) &i);
printf ("test fortuna i = %d \n", i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后编译并链接:
g++ -I/usr/local/include/fortuna -O0 -g3 -Wall -fmessage-length=0 -c test.cpp
g++ -g -L/usr/local/lib/ -o test test.o -lfortuna
test.o: In function `main':
/home/alain/Documents/Poker/WorkSpace/libfortuna/test/test.cpp:14: undefined reference to `fortuna_get_bytes(unsigned int, unsigned char*)'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我也尝试了 …