I am trying to use the function GetAdapterIdentifier but for some reason I keep on getting an error
g++ main.cpp -ld3d9 -ld3dcompiler -lgdi32 -static -static-libstdc++ -o output
main.cpp: In function 'int main()':
main.cpp:22:59: error: cannot call member function 'virtual HRESULT IDirect3D9::GetAdapterIdentifier(UINT, DWORD, D3DADAPTER_IDENTIFIER9*)' without object
22 | HRESULT hResult = IDirect3D9::GetAdapterIdentifier(x ,xWord, &pIdentifier);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
make: *** [Makefile:7: build] Error 1
This is my main.cpp file very simple yet still got an error
#include <iostream>
#include <d3d9.h>
#include <D3D9Types.h>
#include <tchar.h>
int main(void)
{
UINT x;
DWORD xWord;
D3DADAPTER_IDENTIFIER9 pIdentifier;
HRESULT hResult = IDirect3D9::GetAdapterIdentifier(x ,xWord, &pIdentifier);
return 0;
}
I am using a Makefile to compile the main file
CC = g++
FILES = main.cpp
OUT_EXE = output
INCLUDES = -ld3d9 -ld3dcompiler -lgdi32 -static -static-libstdc++
build:$(FILES)
$(CC) $(FILES) $(INCLUDES) -o $(OUT_EXE)
Found this blog that helped me get to this answer
#include <iostream>
#include <d3d9.h>
#include <D3D9Types.h>
#include <tchar.h>
#include <string.h>
LPDIRECT3D9 g_pDirect3D = NULL;
LPDIRECT3DDEVICE9 g_pDirect3D_Device = NULL;
int main(void)
{
UINT x = 0; // Ordinal number that denotes the display adapter.
DWORD xWord = 0 ;
D3DADAPTER_IDENTIFIER9 pIdentifier ;
g_pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
HRESULT hResult = g_pDirect3D->GetAdapterIdentifier(x, xWord, &pIdentifier);
if (hResult == D3D_OK)
std::cout << "Successfully created DirectX handle " << std::endl;
else
std::cout << "Failed to create DirectX handle" << std::endl;
return 0;
}
Direct3DCreate9 creates a handle to call the functions from
Related
I'm working with a device that provides conio.h, conio.c. In my c++ code, whenever I call getch(), I get these errors
/usr/bin/ld: CMakeFiles/hello.dir/main.cpp.o: undefined reference to symbol '_Z5getchv'
//usr/lib/libPhantomIOLib42.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/hello.dir/build.make:96: recipe for target 'hello' failed
make[2]: *** [hello] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/hello.dir/all' failed
make[1]: *** [CMakeFiles/hello.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2
I know conio is not standard but it seems the company addresses the OS issue by
#if defined(WIN32)
#include <windows.h>
#include <conio.h>
#else
#include "conio.h"
#include <string.h"
#endif
where conio.h is
#ifndef __CONIO_H_
#define __CONIO_H_
#ifdef _cplusplus
extern "C" {
#endif // _cplusplus
int _kbhit();
int getch();
#ifdef _cplusplus
}
#endif // _cplusplus
#endif // __CONIO_H
and conio.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/poll.h>
#include <termios.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
static struct termios term_attribs, term_attribs_old;
static void restore_term(void)
{
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attribs_old) < 0)
{
perror("tcsetattr: ");
exit(-1);
}
}
int _kbhit()
{
static int initialized;
fd_set rfds;
struct timeval tv;
int retcode;
if(!initialized)
{
if(tcgetattr(STDIN_FILENO, &term_attribs) < 0)
{
perror("tcgetattr: ");
exit(-1);
}
term_attribs_old = term_attribs;
if(atexit(restore_term))
{
perror("atexit: ");
exit(-1);
}
term_attribs.c_lflag &= ~(ECHO | ICANON | ISIG | IEXTEN);
term_attribs.c_iflag &= ~(IXON | BRKINT | INPCK | ICRNL | ISTRIP);
term_attribs.c_cflag &= ~(CSIZE | PARENB);
term_attribs.c_cflag |= CS8;
term_attribs.c_cc[VTIME] = 0;
term_attribs.c_cc[VMIN] = 0;
if(tcsetattr(STDIN_FILENO, TCSANOW, &term_attribs) < 0)
{
perror("tcsetattr: ");
exit(-1);
}
initialized = 1;
}
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
memset(&tv, 0, sizeof(tv));
retcode = select(1, &rfds, NULL, NULL, &tv);
if(retcode == -1 && errno == EINTR)
{
return 0;
}
else if(retcode < 0)
{
perror("select: ");
exit(-1);
}
else if(FD_ISSET(STDIN_FILENO, &rfds))
{
return retcode;
}
return 0;
}
int getch()
{
fd_set rfds;
int retcode;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
retcode = select(1, &rfds, NULL, NULL, NULL);
if(retcode == -1 && errno == EINTR)
{
return 0;
}
else if(retcode < 0)
{
perror("select: ");
exit(-1);
}
return getchar();
}
My CMakeLists.txt is
cmake_minimum_required(VERSION 3.10)
project(project2 VERSION 0.1.0 LANGUAGES CXX)
set(CMAKE_CXX_FLAGS "-W -O2 -DNDEBUG -Dlinux")
add_executable(hello main.cpp conio.c conio.h)
target_link_libraries(hello HDU HD rt ncurses stdc++ m)
From the conio., I see _cplusplus which should address the c++ code. Why there an error and to fix it? My OS is ubuntu 18.04. One of the Makefiles provided by the company is
CXX=g++
CXXFLAGS+=-W -fexceptions -O2 -DNDEBUG -Dlinux -Iinclude
LIBS = -lHDU -lHD -lrt -lncurses -lstdc++ -lm
TARGET=ServoLoopDutyCycle
HDRS=include/StatsSampler.h
SRCS=src/ServoLoopDutyCycle.cpp \
src/StatsSampler.cpp \
src/conio.c
OBJS=$(patsubst %.cpp,%.o,$(patsubst %.c,%.o,$(SRCS)))
.PHONY: all
all: $(TARGET)
$(TARGET): $(SRCS)
$(CXX) $(CXXFLAGS) -o $# $(SRCS) $(LIBS)
.PHONY: clean
clean:
-rm -f $(OBJS) $(TARGET)
I'm trying to hook some ncurses functions but they don't have any effect.
ncurses isn't statically linked, so I don't see why it wouldn't work.
test.cpp
#include <cstdio>
#include <cstdlib>
#include <curses.h>
int main() {
initscr();
cbreak();
noecho();
getch();
endwin();
return 0;
}
Compiled with: gcc test.cpp -o test -std=c++11 -lncurses
hook.cpp
#include <dlfcn.h>
#include <cstdio>
#include <cstdlib>
int getch() {
typedef int getch ();
getch* old_getch = (getch*) dlsym(RTLD_NEXT, "getch");
int result = old_getch();
fprintf(stderr, "getch() = %i\n", result);
return result;
}
int noecho() {
typedef int noecho ();
noecho* old_noecho = (noecho*) dlsym(RTLD_NEXT, "noecho");
int result = old_noecho();
fprintf(stderr, "noecho() = %i\n", result);
return result;
}
int endwin() {
typedef int endwin ();
endwin* old_endwin = (endwin*) dlsym(RTLD_NEXT, "endwin");
int result = old_endwin();
printf("endwin called");
return result;
}
Compiled with: gcc hook.cpp -o hook.so -shared -ldl -fPIC -std=c++11
It sadly outputs nothing, and I'm completely stumped.
The specification doesn't state getch has to be a function (not a macro). Actually, in ncurses-6.1, getch defined as
#define getch() wgetch(stdscr)
Nonetheless, there is a getch function in libncurses (which simply calls wgetch(stdscr)), so dlsym(libncurses_handle,"getch") does work.
I have a "multiple definition of function" error on f2 function. The same error is in CodeBlocks and MS Visual Studio 2008.
What changes need for this code to work without errors?
Here is the simple example of my program? that has error:
**main.cpp**
#include <iostream>
#include"head.h"
using namespace std;
int main()
{
int n = 5;
cout<<f1(n)<<endl;
cout<<f2(n)<<endl;
return 0;
}
**head.h**
#ifndef HEAD_INCLUDED
#define HEAD_INCLUDED
#include "func1.cpp"
#include "func2.cpp"
int f1(int);
int f2(int);
#endif // HEAD_INCLUDED
**func1.cpp**
#include <iostream>
using namespace std;
int f1(int a)
{
return a+a;
}
**func2.cpp**
#include <iostream>
using namespace std;
int f1(int a)
{
return a+a;
}
-------------- Build: Debug in test (compiler: GNU GCC Compiler)---------------
g++ -Wall -fexceptions -g -c /home/laptop/Documents/CodeBlocksProjects/test/func2.cpp -o obj/Debug/func2.o
g++ -Wall -fexceptions -g -c /home/laptop/Documents/CodeBlocksProjects/test/main.cpp -o obj/Debug/main.o
g++ -o bin/Debug/test obj/Debug/func2.o obj/Debug/main.o
obj/Debug/main.o: In function f2(int)':
/home/laptop/Documents/CodeBlocksProjects/test/func2.cpp:5: multiple definition off2(int)'
obj/Debug/func2.o:/home/laptop/Documents/CodeBlocksProjects/test/func2.cpp:5: first defined here
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 1 second(s))
2 error(s), 0 warning(s) (0 minute(s), 1 second(s))
Try this:
head.h
#ifndef HEAD_INCLUDED
#define HEAD_INCLUDED
int f1(int);
int f2(int);
#endif // HEAD_INCLUDE
head.cpp
#include "head.h"
int f1(int a)
{
return a+a;
}
int f2(int a)
{
return a+a;
}
test1.cpp
#include <iostream>
#include "head.h"
using namespace std;
int main(){
int n = 5;
int val1 = f1(3);
int val2 = f2(5);
cout << "val1 = " << val1 << endl;
cout << "val2 = " << val2 << endl;
return 0;
}
compile and execute commands in linux
g++ -o test1.o head.cpp test1.cpp
./test1.o
The project is all about handshaking Cpp with java For that i am using JNI, I made all the necesaary configuration make Cpp project generate header in src folder of Cpp project create ABC.h file and abc.Cpp file. when i bulid the abc.cpp file it shows error compilation failed due to p.thread no such directory found, if i remove this P.thread header file it shows ABC.h not found while ABC.h file is in same folder and we include on our Cpp file
please help me out how to fix this problem ,i am newbie in JNI
below attached PNG file show you the detail
ERROR ON ECLIPSE CONSOLE//////////////////////////////////////////
00:31:21 **** Incremental Build of configuration Default for project SyntacJNI ****
Info: Internal Builder is used for build
g++ "-IC:\\Program Files\\Java\\jdk1.7.0_21\\include\\win32" "-IC:\\Program Files\\Java\\jdk1.7.0_21\\include" -O2 -g -Wall -c -fmessage-length=0 -o "src\\abc
.o" "..\\src\\abc.cpp"
..\src\abc.cpp:10:21: fatal error:abc.h: No such file or directory
#include <pthread.h>
^
compilation terminated.
00:31:21 Build Finished (took 189ms)
///////////////////////////abc.cpp//////////////CODE Snippet////////////////////////
#include <fstream>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <windows.h>
#include <SyntacInterface.h>
#include <org_syntec_ivb_jni_DeviceCommunicationController.h>
using namespace std;
// An unsigned char can store 1 Bytes (8bits) of data (0-255)
typedef unsigned char BYTE;
// Global Var
bool gLive_View = true;
// Get the size of a file
#include <stdlib.h>
ULONG RcvdBytes;
volatile bool keepRunning = false;
volatile bool modeFlag = true;
static HANDLE hThread = NULL;
int compstr = 0xFF000F00;
long int count = 0;
static void run();
static void end();
static DWORD WINAPI ThreadProc(LPVOID lpParam);
static ULONG BuffSizeLive = 81408;
//CRITICAL_SECTION critical;
int got = 0;
static void run() {
DWORD dummy;
hThread = CreateThread(NULL, 0, ThreadProc, NULL, 0, &dummy);
}
static void end() {
keepRunning = false;
CloseHandle(hThread);
}
bool fexists(const char *filename) {
ifstream ifile(filename);
return ifile.good();
}
static DWORD WINAPI ThreadProc(LPVOID lpParam) {
while (keepRunning) {
BuffSizeLive = 327680;
UCHAR* Buff = (UCHAR*) malloc(sizeof(UCHAR) * BuffSizeLive);
ULONG RcvdBytes;
RcvdBytes = abc_GetCapture(Buff, BuffSizeLive);
if (RcvdBytes == 327680 || RcvdBytes == 81408) {
Syntac_WriteToBinaryFile(Buff,
"C:/listenDir/Capture_Mod1.BIN", RcvdBytes);
}
free(Buff);
cout << "got the frame stream " << got;
}
return 0;
}][1]
I want to use a personal "pthread_self" function. I want to rewrite the "pthread_self()" and, at the final, call to the real "pthread_self".
is it possible in C/C++?
Example:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
pthread_t pthread_self(void)
{
// Do something else.....
// ...
// And finally call to de real pthread_self():
return ::pthread_self(); // This doesn't work
}
main()
{
printf("\nThis is my main thread: %lu\n", pthread_self());
}
the code seems to be C89 without a return type for main. So I suggest you remove the :: operator infront of pthread_self() in the function pthread_self(void) and recompile.
Also call your pthread_self() by some other name such as pthread_self_2(void)
Will it be allowed to overload pthread_self() with a dummy variable (never used)?
pthread_t pthread_self( bool )
{
// Do something else.....
// ...
// And finally call to de real pthread_self():
printf( "%s\n", __PRETTY_FUNCTION__ );
return ::pthread_self(); // This doesn't work
}
int main()
{
printf( "%s\n", __PRETTY_FUNCTION__ );
pthread_self( false );
printf( "%s\n", __PRETTY_FUNCTION__ );
}
produces
$ g++ test.cpp -lpthread
$ ./a.out
int main()
pthread_t pthread_self(bool)
int main()
$
Two other approaches might be:
Write a function my_pthread_self(), use it everywhere, and have it call real pthread_self()
Have a macro PTHREAD_SELF which calls your wrapper which internally calls real pthread_self()
The following is a bit hacky but should work in C. Put the following in a header which you would have to include before or instead of <pthread.h> (but never after pthread.h):
pthread_t _pthread_self(void);
#define pthread_self _pthread_self
And in the corresponding .c file:
#include <pthread>
#include "wrapper.h"
#undef pthread_self // undo the hack locally
pthread_t _pthread_self(void)
{
// Do something else.....
// ...
// And finally call to the real pthread_self():
return pthread_self();
}
Now, when you write pthread_self in some other file than that .c file, it expands to your wrapper function. Within the .c file above, since this hack is not done, the call within the wrapper will call the library function.
This should give you some ideas. If you control the linking of your executable, you don't need to use LD_PRELOAD.
Thanks to all of you.
Based mainly on these two links Intercepting Arbitrary Functions on... and Tutorial: Function Interposition in Linux show by #Carl Norum and #Arkadyi, I show you a possible solution:
I have two source files and a Makefile:
mipthread_self.C:
#include <stdio.h>
#include <pthread.h>
#if defined (__STDC__) || defined (__cplusplus) || defined (c__plusplus)
#if defined (__cplusplus) || defined (c__plusplus)
extern "C" {
#endif
pthread_t __real_pthread_self();
pthread_t __wrap_pthread_self(void)
{
printf("This is mipthread_self %lu\n", __real_pthread_self());
return __real_pthread_self();
}
#if defined (__cplusplus) || defined (c__plusplus)
}
#endif
#endif
test.C:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
void* start_function(void* value)
{
printf("Thread start function is running for: %u\n", (unsigned int)pthread_self());
sleep(5);
pthread_exit(value);
}
int main()
{
int res;
pthread_t thread1, thread2;
void* threadReturnValue;
res = pthread_create(&thread1, NULL, start_function, (void*)"thread-one");
if (res != 0) {
perror("Creation of thread failed");
exit(EXIT_FAILURE);
}
printf("Thread1 created with id: %u\n", (unsigned int)thread1);
res = pthread_create(&thread2, NULL, start_function, (void*)"thread-two");
if (res != 0) {
perror("Creation of thread failed");
exit(EXIT_FAILURE);
}
printf("Thread2 created with id: %u\n", (unsigned int)thread2);
res = pthread_join(thread1, &threadReturnValue);
if (res != 0) {
perror("Joining of thread failed");
exit(EXIT_FAILURE);
}
printf("%s joined.\n", (char*)threadReturnValue);
res = pthread_join(thread2, &threadReturnValue);
if (res != 0) {
perror("Joining of thread failed");
exit(EXIT_FAILURE);
}
printf("%s joined.\n", (char*)threadReturnValue);
return 0;
}
And the Makefile:
BIN=test
CFLAGS=-g -m32 -fPIC -Wall $(DEFINES)
TIPO=-m32
PWD=`pwd`
DIR_OBJ=$(PWD)
DIR_BIN=$(DIR_OBJ)
INCLUDES=
LD_LIBRARIES=-L$(DIR_OBJ) -lmipthread -Xlinker --wrap -Xlinker pthread_self -lpthread -lstdc++
$(BIN): libmipthread.a
#echo ""
#echo "Se va a generar el binario $#"
#echo ""
gcc $(CFLAGS) test.C -o $(DIR_BIN)/$# $(LD_LIBRARIES)
libmipthread.a: mipthread_self.o
ar crv $(DIR_OBJ)/$# $(DIR_OBJ)/$<
mipthread_self.o: mipthread_self.C
gcc $(CFLAGS) -c ${PWD}/$< -o $(DIR_OBJ)/$# $(INCLUDES)
clean:
rm $(DIR_OBJ)/*.o
rm $(DIR_OBJ)/*.a
rm $(DIR_OBJ)/test