My gcc version is 9.4.0, Clang 7.0.1 and C++17.
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
auto const path_of_friend = fs::absolute( "path/friend" );
}
And I am getting the error "undefined reference to `std::filesystem::absolute".
What must I do?
Related
I am trying to create an interface for my program using SDL2. The problem is that when I include my .h file, the compiler can't find the .cpp even tough I linked everything. None of the methods I found on the internet work and when compiling in visual studio, the code works perfectly fine. What is more strange is that when I try to make my classes inherit from others, I get the same error from the parent class. I am also using the latest version of Ubuntu. Furthermore, I installed all the libraries required for SDL2.
This is the error:
/usr/bin/ld: /tmp/ccWHZ70h.o: in function `interface::init(char const*)':
interface.cpp:(.text+0xd6): undefined reference to `interface::renderer'
/usr/bin/ld: interface.cpp:(.text+0xdd): undefined reference to `interface::renderer'
/usr/bin/ld: interface.cpp:(.text+0x106): undefined reference to `interface::renderer'
/usr/bin/ld: /tmp/ccWHZ70h.o: in function `interface::LoadTexture(char const*)':
interface.cpp:(.text+0x1bd): undefined reference to `interface::renderer'
/usr/bin/ld: /tmp/ccWHZ70h.o: in function `interface::DrawTexture(SDL_Texture*, SDL_Rect, SDL_Rect)':
interface.cpp:(.text+0x20d): undefined reference to `interface::renderer'
collect2: error: ld returned 1 exit status
make: *** [MakeFile:2: all] Error 1
I compile using make -f on this file:
all:
g++ mainClient.cpp client.cpp interface.cpp -lSDL2main -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf -o client.exe
clean:
rm -f *~client-exe
The code works perfectly fine without the interface class.
This is the inteface.h code:
#pragma once
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_render.h>
#include <iostream>
class interface
{
private:
int width, height;
SDL_Window* window;
bool state;
static SDL_Renderer* renderer;
public:
interface(int w,int h);
~interface(){};
int init(const char* titlu);
void loop();
void render();
SDL_Texture* LoadTexture(const char* filename);
void DrawTexture(SDL_Texture* tex, SDL_Rect imgRect, SDL_Rect destRect);
};
And this is the interface.cpp code:
#include "interface.h"
interface::interface(int w, int h)
{
width = w;
height = h;
state = true;
}
int interface::init(const char* title)
{
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
window = SDL_CreateWindow(title,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,0);
if(window == NULL)
{
std::cout<<"Eroare la crearea ferestrei!\n";
state = false;
}
renderer = SDL_CreateRenderer(window,-1,0);
if(renderer == NULL)
{
std::cout<<"Eroare la crearea rendererului!\n";
state = false;
}
else
{
SDL_SetRenderDrawColor(renderer,255,255,255,255);
}
}
else
{
std::cout<<"ERROR AT INIT EVERYTHING\n";
state = false;
}
}
SDL_Texture* interface::LoadTexture(const char* filename)
{
SDL_Surface* tempSurface = IMG_Load(filename);
if(!tempSurface)
{
std::cout<<"Eroare la imaginea: "<<filename<<SDL_GetError<<std::endl;
}
SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer,tempSurface);
SDL_FreeSurface(tempSurface);
return tex;
}
void interface::DrawTexture(SDL_Texture* tex, SDL_Rect imgRect,SDL_Rect destRec)
{
SDL_RenderCopyEx(renderer,tex,&imgRect,&destRec,0,NULL,SDL_FLIP_NONE);
}
This is the code of mainClient.cpp where I include interface.h:
#include "client.h"
#include "interface.h"
int main()
{
client *Client = new client;
interface *screen = new interface(800,600);
screen->init("ReadsProfiler");
Client->init();
Client->loop();
}
I get the same error if I don't use client.h, so I'm not gonna add the code to that as well.
Because rendereris static, you also need to declare it in the .cpp file.
Add it to interface.cpp, for example under #include "interface.h":
SDL_Renderer* interface::renderer;
I am new to c programming on Linux. My objective is to create an efficient program to monitor free space on a mounted partition (parm1) and put an icon in the tray to represent it showing my name (parm2) for it and the percentage of free space (script to calculate parm3), run that script every xxx seconds (parm4) and refresh the icon if the value changes (icon filename parm5).
For starters I have just copied a sample c program from here and am just trying to get it to compile and run as is, to embed a program with an icon in the tray.
freedesktop XEmbed systray client code in C, Xlib
I tweaked it to incorporate suggested changes and remove errors, and it compiles clean, but appears to need lib(s) added to be able to create an executable.
How do I find out what libraries are needed, and what order to put them in?
/* from: https://stackoverflow.com/questions/45392284/freedesktop-xembed-systray-client-code-in-c-xlib */
#include <X11/Xutil.h>
#include <string.h>
#include <unistd.h>
#define MIN(A, B) ((A) < (B) ? (A) : (B))
/* --------- XEMBED and systray stuff */
#define SYSTEM_TRAY_REQUEST_DOCK 0
#define SYSTEM_TRAY_BEGIN_MESSAGE 1
#define SYSTEM_TRAY_CANCEL_MESSAGE 2
int usleep(useconds_t useconds); //pass in microseconds
static int trapped_error_code = 0;
static int (*old_error_handler) (Display *, XErrorEvent *);
static int
error_handler(Display *display, XErrorEvent *error) {
trapped_error_code = error->error_code;
return 0;
}
void
trap_errors(void) {
trapped_error_code = 0;
old_error_handler = XSetErrorHandler(error_handler);
}
int
untrap_errors(void) {
XSetErrorHandler(old_error_handler);
return trapped_error_code;
}
void
send_systray_message(Display* dpy, long message, long data1, long data2, long data3) {
XEvent ev;
Atom selection_atom = XInternAtom (dpy,"_NET_SYSTEM_TRAY_S0",False);
Window tray = XGetSelectionOwner (dpy,selection_atom);
if ( tray != None)
XSelectInput (dpy,tray,StructureNotifyMask);
memset(&ev, 0, sizeof(ev));
ev.xclient.type = ClientMessage;
ev.xclient.window = tray;
ev.xclient.message_type = XInternAtom (dpy, "_NET_SYSTEM_TRAY_OPCODE", False );
ev.xclient.format = 32;
ev.xclient.data.l[0] = CurrentTime;
ev.xclient.data.l[1] = message;
ev.xclient.data.l[2] = data1; // <--- your window is only here
ev.xclient.data.l[3] = data2;
ev.xclient.data.l[4] = data3;
trap_errors();
XSendEvent(dpy, tray, False, NoEventMask, &ev);
XSync(dpy, False);
usleep(10000);
if (untrap_errors()) {
/* Handle errors */
}
}
/* ------------ Regular X stuff */
int
main(int argc, char **argv) {
int width, height;
XWindowAttributes wa;
XEvent ev;
Display *dpy;
int screen;
Window root, win;
/* init */
if (!(dpy=XOpenDisplay(NULL)))
return 1;
screen = DefaultScreen(dpy);
root = RootWindow(dpy, screen);
if(!XGetWindowAttributes(dpy, root, &wa))
return 1;
width = height = MIN(wa.width, wa.height);
/* create window */
win = XCreateSimpleWindow(dpy, root, 0, 0, width, height, 0, 0, 0xFFFF9900);
/* call send_systray_messsage */
send_systray_message(dpy, SYSTEM_TRAY_REQUEST_DOCK, win, 0, 0); // pass win only once
XMapWindow(dpy, win);
XSync(dpy, False);
/* run */
while(1) {
while(XPending(dpy)) {
XNextEvent(dpy, &ev); /* just waiting until we error because window closed */
}
}
}
$ gcc xmbed_system_tray.c -o xmbed_system_tray
/usr/bin/ld: /tmp/ccs2ZnYg.o: in function `trap_errors':
xmbed_system_tray.c:(.text+0x3a): undefined reference to `XSetErrorHandler'
/usr/bin/ld: /tmp/ccs2ZnYg.o: in function `untrap_errors':
xmbed_system_tray.c:(.text+0x57): undefined reference to `XSetErrorHandler'
/usr/bin/ld: /tmp/ccs2ZnYg.o: in function `send_systray_message':
xmbed_system_tray.c:(.text+0xa8): undefined reference to `XInternAtom'
/usr/bin/ld: xmbed_system_tray.c:(.text+0xc2): undefined reference to `XGetSelectionOwner'
/usr/bin/ld: xmbed_system_tray.c:(.text+0xe8): undefined reference to `XSelectInput'
/usr/bin/ld: xmbed_system_tray.c:(.text+0x131): undefined reference to `XInternAtom'
/usr/bin/ld: xmbed_system_tray.c:(.text+0x1ab): undefined reference to `XSendEvent'
/usr/bin/ld: xmbed_system_tray.c:(.text+0x1bf): undefined reference to `XSync'
/usr/bin/ld: /tmp/ccs2ZnYg.o: in function `main':
xmbed_system_tray.c:(.text+0x1f3): undefined reference to `XOpenDisplay'
/usr/bin/ld: xmbed_system_tray.c:(.text+0x24f): undefined reference to `XGetWindowAttributes'
/usr/bin/ld: xmbed_system_tray.c:(.text+0x2b2): undefined reference to `XCreateSimpleWindow'
/usr/bin/ld: xmbed_system_tray.c:(.text+0x2ed): undefined reference to `XMapWindow'
/usr/bin/ld: xmbed_system_tray.c:(.text+0x2fe): undefined reference to `XSync'
/usr/bin/ld: xmbed_system_tray.c:(.text+0x316): undefined reference to `XNextEvent'
/usr/bin/ld: xmbed_system_tray.c:(.text+0x322): undefined reference to `XPending'
collect2: error: ld returned 1 exit status
I didn't find any videos that showed anything, but I did find a site where he shows possible libraries or maybe locations. Therefore, I am guessing that either my environment is setup wrong so it can't find what it's looking for, or what it's looking for isn't installed.
http://mech.math.msu.su/~nap/2/GWindow/xintro.html
That didn't help, but I eventually found another page that did at:
https://en.wikibooks.org/wiki/X_Window_Programming/Xlib
Their sample program had compile instructions:
To Compile: gcc -O2 -Wall -o test test.c -L /usr/X11R6/lib -lX11 -lm
So for mine I tried:
gcc -o xmbed_system_tray xmbed_system_tray.c -lX11
By removing options one at a time I found the -lX11 parameter not being there before was causing the failure, and now the executable gets created and will run.
The program only consumes about 1 mb of memory compared to 25 to 35 mb for the other programs I found capable of running a program in the tray.
my vm system is ubuntu 16.04, and i have download the kernel header files,i have write the code,but don't know how to edit gcc code or Makefile
#include <linux/netdevice.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
int main(){
struct net_device *dev = dev_base;
struct in_device *mydevice;
struct in_ifaddr *myifaddr;
while( dev != NULL ){
printf("dev name: %s\n", dev->name);
mydevice = dev->ip_ptr;
myifaddr = mydevice->ifa_list;
while(myifaddr != NULL){
printf("ip: %s\n", inet_ntoa((struct in_addr)(myifaddr->ifa_local)));
myifaddr = myifaddr->ifa_next;
}
dev = dev->next;
}
return 0;
}
if i use gcc test.c it will come out some error like this
yq#ubuntu:~/test$ gcc test.c
In file included from /usr/include/linux/netdevice.h:28:0,
from test.c:1:
/usr/include/linux/if.h:234:19: error: field ‘ifru_addr’ has incomplete type
struct sockaddr ifru_addr;
^
......
test.c: In function ‘main’:
test.c:8:27: error: ‘dev_base’ undeclared (first use in this function)
struct net_device *dev = dev_base;
I encountered an error when I am trying to link the Fbx static library (libfbxsdk.a) to a project on Linux using CMake that keeps reporting for undefined references to fbx-related functions. I checked the configuration in the CMakelist and everything looks alright, but the program cannot be compiled statically or dynamically. I really don't have any idea why the static library is not linked properly and appreciate any helps to fix the linking issue. Here is my cmakelist file for static linking:
cmake_minimum_required(VERSION 3.6)
SET(CMAKE_VERBOSE_MAKEFILE ON)
project(FBX_SDK)
add_definitions(-DFBXSDK_NEW_API)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lm -lrt -luuid -lstdc++ -lpthread -ldl -lX11 -std=c++11")
include_directories(${X11_LIBRARIES}
${FBX_SDK_SOURCE_DIR}/fbxsdk-1.2/include/)
link_directories(${X11_LIBRARIES}
${FBX_SDK_SOURCE_DIR}/fbxsdk-1.2/Lib/Linux/Debug_x64/)
set(SOURCE_FILES main.cpp)
add_executable(FBX_SDK ${SOURCE_FILES})
target_link_libraries(FBX_SDK
${FBX_SDK_SOURCE_DIR}/fbxsdk-1.2/Lib/Linux/Debug_x64/libfbxsdk.a)
and my cpp file is as below:
#include <iostream>
#include <fbxsdk.h>
void InitializeSDKObjects(FbxManager* &pManager, FbxScene* &pScene);
int main()
{
std::cout << "Hello, World!" << std::endl;
FbxManager* lSdkManager = NULL;
FbxScene* lScene = NULL;
bool lResult;
InitializeSDKObjects(lSdkManager, lScene);
return 0;
}
void InitializeSDKObjects(FbxManager* &pManager, FbxScene* &pScene)
{
pManager = FbxManager::Create();
if(!pManager)
{
FBXSDK_printf("Error: Unable to create FBX Manager!\n");
exit(1);
}
else FBXSDK_printf("Autodesk FBX SDK version %s\n", pManager->GetVersion());
FbxIOSettings* ios = FbxIOSettings::Create(pManager, IOSROOT);
pManager->SetIOSettings(ios);
FbxString lPath = FbxGetApplicationDirectory();
pManager->LoadPluginsDirectory(lPath.Buffer());
pScene = FbxScene::Create(pManager, "My Scene");
if(!pScene)
{
FBXSDK_printf("Error: Unable to create FBX scene!\n");
exit(1);
}
}
While the include and lib folders of fbx sdk are like this:
I'm trying to use the lib icu in my shared library.
So i generate my .so who use libicu functions, this parts works well.
Then i try to compile my main program, using the shared library that i created before. But i get the following error:
release//libstring.so: undefined reference to `u_errorName_53'
release//libstring.so: undefined reference to `ucnv_toUnicode_53'
release//libstring.so: undefined reference to `ucnv_close_53'
release//libstring.so: undefined reference to `ucnv_fromUChars_53'
release//libstring.so: undefined reference to `udat_open_53'
release//libstring.so: undefined reference to `udat_parse_53'
release//libstring.so: undefined reference to `udat_format_53'
release//libstring.so: undefined reference to `ucnv_toUChars_53'
release//libstring.so: undefined reference to `ucnv_open_53'
release//libstring.so: undefined reference to `udat_close_53'
I have installed the libicu with this help.
I compile my shared library with -licuuc, as well as my main program compile only with -l:libstring.so and -licuuc.
When i do ldd libstring.so, i don't have the .so of the libicu on the list of dependencies.
The problem probably come from this.
Thx
EDIT: I have create a little test:
icu.h:
#pragma once
#include <iostream>
#include <string>
#include <unicode/ucnv.h>
class c_icu
{
private:
void * p_priv;
public:
c_icu();
void open(const wchar_t *name);
void to_local(std::wstring &, std::wstring &);
void from_local(std::wstring &, std::wstring &);
void close(void);
~c_icu();
};
icu.cpp:
#include "icu.h"
c_icu::c_icu()
{
p_priv = NULL;
}
c_icu::~c_icu()
{
if (p_priv)
close();
}
void c_icu::open(const wchar_t *name)
{
UErrorCode status = U_ZERO_ERROR;
char conv_name[256];
if (wcstombs(conv_name, name, 256) == (size_t)-1)
{
std::cout << "ERROR: wcstombs failed" << std::endl;
exit(0);
}
if ((p_priv = (UConverter *)ucnv_open(conv_name, &status)) == NULL)
{
std::cout << "ERROR: ucnv_open failed" << std::endl;
exit(0);
}
}
void c_icu::to_local(std::wstring &src, std::wstring &dst)
{
UErrorCode status = U_ZERO_ERROR;
int32_t len;
len = ucnv_toUChars((UConverter *)p_priv, (UChar *)NULL, 0, (const char *)src.c_str(), src.size(), &status);
if (status == U_BUFFER_OVERFLOW_ERROR)
{
status = U_ZERO_ERROR;
dst.resize(len);
len = ucnv_toUChars((UConverter *)p_priv, (UChar *)dst.c_str(), dst.size(), (const char *)src.c_str(), src.size(), &status);
if (U_FAILURE(status))
{
std::cout << "ERROR: to_local failed" << std::endl;
exit(0);
}
}
}
void c_icu::from_local(std::wstring &src, std::wstring &dst)
{
UErrorCode status = U_ZERO_ERROR;
int32_t len;
len = ucnv_fromUChars((UConverter *)p_priv, (char *)NULL, 0, (const UChar *)src.c_str(), src.size(), &status);
if (status == U_BUFFER_OVERFLOW_ERROR)
{
status = U_ZERO_ERROR;
dst.reserve(len);
len = ucnv_fromUChars((UConverter *)p_priv, (char *)dst.c_str(), dst.size(), (const UChar *)src.c_str(), -1, &status);
}
}
void c_icu::close(void)
{
ucnv_close((UConverter *)p_priv);
p_priv = NULL;
}
test.cpp:
#include "icu.h"
int main(void)
{
c_icu converter;
std::wstring src = L"";
std::wstring dst;
const wchar_t add[11] = L"Just a test";
const wchar_t *unicode = L"koi8-r";
for (unsigned int i = 0; i < 1000; i++)
src.append(add, 11);
converter.open(unicode);
converter.from_local(src, dst);
converter.close();
}
Makefile:
NAME= test
LIB_NAME= libicutest.so
FLAGS= -W -Wall -Wextra
all: lib $(NAME)
$(NAME):
#echo "Main programm compiling ..."
g++ -c test.cpp -o test.o $(FLAGS)
g++ -o $(NAME) test.o -Wl,-rpath,'$$ORIGIN/' -licui18n -L. -licutest
#echo "Main programm compiled"
lib:
#echo "Lib Compiling ..."
g++ -std=gnu++11 -c -fPIC icu.cpp -o icu.o $(FLAGS)
g++ -shared -Wl,-soname,$(LIB_NAME) -o $(LIB_NAME) icu.o -licui18n
#echo "Lib Compiled"
Note: even with -licuuc instead of -licui18n i got the same error.
Output:
$> make
Lib Compiling ...
g++ -std=gnu++11 -c -fPIC icu.cpp -o icu.o -W -Wall -Wextra
g++ -shared -Wl,-soname,libicutest.so -o libicutest.so icu.o -licui18n
Lib Compiled
Main programm compiling ...
g++ -c test.cpp -o test.o -W -Wall -Wextra
g++ -o test test.o -Wl,-rpath,'$ORIGIN/' -licui18n -L. -licutest
./libicutest.so: undefined reference to `ucnv_close_53'
./libicutest.so: undefined reference to `ucnv_fromUChars_53'
./libicutest.so: undefined reference to `ucnv_toUChars_53'
./libicutest.so: undefined reference to `ucnv_open_53'
collect2: error: ld returned 1 exit status
make: *** [test] Error 1
If i do ldd on my shared library:
$> ldd libicutest.so
linux-vdso.so.1 => (0x00007ffd5eb7f000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f092b5e6000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f092b221000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f092af1a000)
/lib64/ld-linux-x86-64.so.2 (0x00007f092bb00000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f092ad04000)
EDIT2:
Ok, seems that the problem is easier to solve than expected. My program need function in icu_53, but i got icu_48 installed on my computer dunno why.
$> locate libicu
/usr/lib/x86_64-linux-gnu/libicudata.so.48
/usr/lib/x86_64-linux-gnu/libicudata.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicui18n.so.48
/usr/lib/x86_64-linux-gnu/libicui18n.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicuio.so.48
/usr/lib/x86_64-linux-gnu/libicuio.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicule.so.48
/usr/lib/x86_64-linux-gnu/libicule.so.48.1.1
/usr/lib/x86_64-linux-gnu/libiculx.so.48
/usr/lib/x86_64-linux-gnu/libiculx.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicutest.so.48
/usr/lib/x86_64-linux-gnu/libicutest.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicutu.so.48
/usr/lib/x86_64-linux-gnu/libicutu.so.48.1.1
/usr/lib/x86_64-linux-gnu/libicuuc.so.48
/usr/lib/x86_64-linux-gnu/libicuuc.so.48.1.1
Does anyone know how to upgrade the version from icu_48 to icu_53 at least ?
Seems 'udat_parse_53' is located inside libicui18n.so.53 library. Try to compile your project with '-licui18n' option.