Can't build very simple tcl application - tcl

I'm trying to write a very simple Tcl application in C++:
#include <tcl.h>
#include <iostream>
int main (int argc, char *argv[])
{
std::cout << "Calling Tcl_FindExecutable." << std::endl;
Tcl_FindExecutable (argv[0]);
std::cout << "Calling Tcl_CreateInterp." << std::endl;
Tcl_Interp *pInterp = Tcl_CreateInterp ();
if (Tcl_Eval (pInterp, "puts stdout {Hello, World!}") != TCL_OK)
{
std::cerr << "Error: " << Tcl_GetStringResult (pInterp) << std::endl;
return (0);
}
if (Tcl_Eval (pInterp, "puts stdout [info nameofexecutable]") != TCL_OK)
{
std::cerr << "Error: " << Tcl_GetStringResult (pInterp) << std::endl;
return (0);
}
return (1);
}
I can compile it via g++ -c Wall -I/opt/ActiveTcl-8.6/include noddy.cpp -o noddy.o
but when I link it, with g++ -L/opt/ActiveTcl-8.6/lib -ltcl8.6 -o noddy noddy.o
I get errors saying that all the Tcl library procedures are undefined.
What am I doing wrong, please?
Edit
The actual commands were
$ g++ -c -Wall -I/opt/ActiveTcl-8.6/include noddy.cpp -o noddy.o
$ g++ -L/opt/ActiveTcl-8.6/lib -ltcl8.6 -o noddy noddy.o
noddy.o: In function 'main':
noddy.cpp:(.text+0x37): undefined reference to 'Tcl_FindExecutable'
noddy.cpp:(.text+0x60): undefined reference to 'Tcl_CreateInterp'
noddy.cpp:(.text+0x78): undefined reference to 'Tcl_Eval'
noddy.cpp:(.text+0x8d): undefined reference to 'Tcl_GetStringResult'
noddy.cpp:(.text+0xda): undefined reference to 'Tcl_Eval'
noddy.cpp:(.text+0xef): undefined reference to 'Tcl_GetStringResult'
collect2: ld returned 1 exit status

In link statements the order of object modules and libraries is significant. You should include the object first and then the libraries (redone as C to avoid installing g++):
> gcc -Wall -I/opt/tcl/include -c noddy.c
> gcc -o noddy.exe noddy.o -L/opt/tcl/lib -ltcl86
> noddy
Calling Tcl_FindExecutable.
Calling Tcl_CreateInterp.
Hello, World!
C:/Code/noddy.exe
But:
> gcc -o noddy.exe -L/opt/tcl/lib -ltcl86 noddy.o
noddy.o:noddy.c:(.text+0x23): undefined reference to '_imp__Tcl_FindExecutable'
noddy.o:noddy.c:(.text+0x36): undefined reference to '_imp__Tcl_CreateInterp'
noddy.o:noddy.c:(.text+0x50): undefined reference to '_imp__Tcl_Eval'
noddy.o:noddy.c:(.text+0x62): undefined reference to '_imp__Tcl_GetStringResult'
noddy.o:noddy.c:(.text+0x9b): undefined reference to '_imp__Tcl_Eval'
noddy.o:noddy.c:(.text+0xad): undefined reference to '_imp__Tcl_GetStringResult'
e:/opt/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: noddy.o: bad reloc address 0x20 in section '.eh_frame'
e:/opt/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status

Related

Static linking with cublas

I want to link my program with the static version of cublas, but I get some undefined references. The command and error are
$ nvcc test.cu -o test --cudart=static -ldl -lpthread -lcurand_static -lcublas_static -lculibos
/home/mahmood/cuda_10.1.168/bin/../targets/x86_64-linux/lib/libcublas_static.a(cublas.o): In function `cublasCtxInit(cublasContext**)':
cublas.compute_75.cudafe1.cpp:(.text+0x34b): undefined reference to `cublasLtCtxInit'
cublas.compute_75.cudafe1.cpp:(.text+0x417): undefined reference to `init_gemm_select'
...
...
In fact, the library path is fine and the cublasLtCtxInit exists in the static library file.
$ ls -l /home/mahmood/cuda_10.1.168/lib64/libcublas_static.a
-rw-rw-r-- 1 mahmood mahmood 75127082 Jun 27 16:06 /home/mahmood/cuda_10.1.168/lib64/libcublas_static.a
$ grep cublasLtCtxInit ~/cuda_10.1.168/lib64/libcublas_static.a
Binary file /home/mahmood/cuda_10.1.168/lib64/libcublas_static.a matches
$ echo $LD_LIBRARY_PATH
/home/mahmood/cuda_10.1.168/lib64:
So, how can I fix that?
The correct static linking sequence with cublas can be found in the Makefile for the conjugateGradient CUDA sample code.
The needed switches for nvcc are:
-lcublas_static -lcublasLt_static -lculibos
example:
$ cat t1752.cu
#include <cublas_v2.h>
int main(){
cublasHandle_t h;
cublasCreate(&h);
}
$ nvcc t1752.cu -o t1752 -lcublas_static -lcublasLt_static -lculibos
$

Add MySQL to my Makefile gives problems

I have a problem with my C project built under Debian Jessie. After doing some stuffs now I need to work with MySQL so i download the library and try to update my Makefile.
This is my Makefile right now
CC = gcc
CFLAGS = -Wall -Wextra
LDFLAGS = -lbluetooth -lpthread -lmysqlclient
LFLAGS = -lm
INC = -I/usr/include/mysql
SOURCES = stb.c btscan.c and.c gima.c database.c
OBJECTS = $(SOURCES:.c=.o)
EXECUTABLE = stb
$(EXECUTABLE): $(OBJECTS)
$(CC) $(CFLAGS) $(OBJECTS) -o $# $(LFLAGS) $(LDFLAGS)
stb.o: btscan.h and.h gima.h database.h
btscan.o: btscan.h
and.o: and.h
gima.o: gima.h
database.o: database.h
clean:
#rm -f *.o *.out stb
and this is the files where I want to use the library
stb.c
#include "btscan.h"
#include "and.h"
#include "gima.h"
#include "database.h"
struct device* bt_devices;
struct device* ble_devices;
int main(void) {
//-------------------------------- DATABASE CONNECTION ----------------
//MYSQL *con = mysql_init(NULL);
....... }
and finally database.h
#ifndef _DATABASE_H
#define _DATABASE_H
#include <my_global.h>
#include <mysql.h>
#endif
When I try to make i receive "Fatal error : my_global.h No such file or directory". However if i try MySQL on a single test file and compiling it with
gcc -o test test.c -I/usr/include/mysql -lmysqlclient
it works. Where I made a mistake?
Thanks in advance
If you'd shown us the output of the compiler, or examine it yourself, you'll immediately see that what you're running on the command line is not the same at all as what make is running: in particular make is not adding the -I/usr/include/mysql flag to the command line.
That's because in your makefile you set:
INC = -I/usr/include/mysql
but nowhere in your makefile (as you've shown it) is the variable INC actually used, so this is essentially a no-op.
Since you're using the standard GNU make compilation rules, you should be setting standard GNU make variables:
CPPFLAGS = -I/usr/include/mysql

Print Universal Characters (html) stored in a string c++

I am trying to print a string with universal characters stored in it. If i initialized the string with the following:
string test = "\u000D\u000A\u000D\u000Aclass Solution {\u000D\u000Apublic:\u000D\u000A
cout << test << endl;
it would print out the output I want:
class Solution {
but if I get the same string from Curl result, like following:
curl_easy_setopt(curl, CURLOPT_WRITEDATA, addressof(test));
res = curl_easy_perform(curl);
cout << test << endl;
it would print out:
\u000D\u000A\u000D\u000Aclass Solution {\u000D\u000Apublic:\u000D\u000A
I tried to turn the test into const char * by c_str and then print it out, but it is still not working. I tried to google it for a few hours but unfortunately I cannot find the answer.
I am using Mac and compile with
clang++ -std=c++11 -Wall -Wextra -lcurl
May anyone help me on this?
Thanks a lot!
Just in case anyone facing the same issue, I get this done by using ICU library with UnicodeString Converter.

Dynamic Parallelism - separate compilation: undefined reference to __cudaRegisterLinkedBinary

Although I have followed apendix C "Compiling Dynamic Parallelism" from "CUDA Programming Guide" and the solutions given here, I cannot manage to solve the problem I have. After the compilation and linking (make DivideParalelo) I get the following error:
./build/metodos.o: In function `__sti____cudaRegisterAll_42_tmpxft_00002599_00000000_6_metodos_cpp1_ii_32c9141e()':
tmpxft_00002599_00000000-3_metodos.cudafe1.cpp:(.text.startup+0x15): undefined reference to `__cudaRegisterLinkedBinary_42_tmpxft_00002599_00000000_6_metodos_cpp1_ii_32c9141e'
./build/GPUutil.o: In function `__sti____cudaRegisterAll_42_tmpxft_000025c0_00000000_6_GPUutil_cpp1_ii_f81fb8b5()':
tmpxft_000025c0_00000000-3_GPUutil.cudafe1.cpp:(.text.startup+0x15): undefined reference to `__cudaRegisterLinkedBinary_42_tmpxft_000025c0_00000000_6_GPUutil_cpp1_ii_f81fb8b5'
./build/PCA_Kernels.o: In function `__sti____cudaRegisterAll_46_tmpxft_000025e6_00000000_6_PCA_Kernels_cpp1_ii_8a59b72a()':
tmpxft_000025e6_00000000-3_PCA_Kernels.cudafe1.cpp:(.text.startup+0x15): undefined reference to `__cudaRegisterLinkedBinary_46_tmpxft_000025e6_00000000_6_PCA_Kernels_cpp1_ii_8a59b72a'
./build/DivideParalelo.o: In function `__sti____cudaRegisterAll_49_tmpxft_0000260c_00000000_6_DivideParalelo_cpp1_ii_16d0a16f()':
tmpxft_0000260c_00000000-3_DivideParalelo.cudafe1.cpp:(.text.startup+0x385): undefined reference to `__cudaRegisterLinkedBinary_49_tmpxft_0000260c_00000000_6_DivideParalelo_cpp1_ii_16d0a16f'
make: *** [DivideParalelo] Error 1
A simplified version of my code is listed below.
DivideParalelo.cu:
#include <stdio.h> #include <string.h>
/*C includes*/
extern"C" {
#include"io.h"
#include"util.h"
}
/* CUDA includes*/
#include"cuda.h"
#include"cublas.h"
#include"metodos.h"
#define CUDA_CHECK_RETURN(value) {
/...
}
#define DIM 100
/*
* image
* num_bands
* columns initially is lines_samples, later the number of endmembers
*/
__global__ void Divide(double *image, int num_bands, int columns, int DIM_MIN, int numColsLastPiece, double *out, double *piece) {
int tid=threadIdx.x; //col
int bid=blockIdx.x; //row
for (int tile=0;tile<(columns -1)/ DIM_MIN +1;tile++) {
__shared__ double sh_piece[DIM];
//some code here...
__syncthreads();
}
int mat=HYSIME(piece,columns,num_bands);
}
}
int main(int argc,
char** argv) {
//load file (argv[1]) with the image into dMt
//...
//Allocate GPU memory:
double *devicedM, *deviceOut;
CUDA_CHECK_RETURN(cudaMalloc((void**)&devicedM, num_bands*lines_samples*sizeof(double)));
CUDA_CHECK_RETURN(cudaMalloc((void**)&deviceOut, num_bands*lines_samples*sizeof(double)));
//here the call to the kernel
}
metodos.cu:
extern "C"{
#include "util.h"
#include "io.h"
}
#include "cuda.h"
#include "cublas.h"
#include "PCA_Kernels.h"
#include "GPUutil.h"
#include <stdio.h>
__device__ __host__ int HYSIME(double *M, int lines_samples, int num_bands){
int N_END =0;
double *y;
double *w;
double *Rw;
y = (double*) malloc(lines_samples * num_bands * sizeof(double));
//changed to implement calloc in the device:
w = (double*) malloc(lines_samples * num_bands*sizeof(double));
memset (w,0,lines_samples * num_bands);
Rw = (double*) malloc(num_bands * num_bands* sizeof(double));
memset (Rw,0,num_bands * num_bands);
//some additional code here
estNoise(y, w, Rw, num_bands, lines_samples);//GPUutil.cu
return(N_END);
}
GPUutil.cu:
#include "cublas.h"
#include "cuda.h"
#include "cuda_runtime.h"
__device__ __host__ int destAdditiveNoise(double *r, double *w, double *Rw, int L, int N){
//the code
return (0);
}
__device__ __host__ int estNoise(double *y, double *w, double *Rw, int L, int N){
//the code
return (0);
}
__device__ __host__ int hysime(double *y, double *w, double *Rw, int L, int N){ //L is num_bands N is lines_samples
//the code
return(0);
}
Makefile:
MKL =1
#initial definitions (library paths et al.)
CUDA_PATH=/usr/local/cuda-6.5
MKLROOT=/home/emartel/intel/composer_xe_2015.0.090/mkl
BUILD_DIR=./build
####################
#includes
####################
#Cuda includes
CUDA_INCLUDE_DIR=-I. -I$(CUDA_PATH)/include
#-I$(SDK)/C/common/inc
#BLAS includes
BLAS_INCLUDE_DIR=-I. -I$(MKLROOT)/include
####################
#library search paths
####################
CUDA_LIB_DIR=-L$(CUDA_PATH)/lib64
#-L$(SDK)/C/lib -L$(SDK)/C/common/lib/linux
BLAS_LIB_DIR=-L$(MKLROOT)/lib/intel64 -L$(MKLROOT)/../compiler/lib/intel64
####################
#libraries
####################
CUDALIBS=-lcublas -lcudart
#-lcutil
#-lGL -lGLU
utilS= -lpthread -lm
####################
#other compilation flags
####################
CFLAGS= -Wwrite-strings
#-Wall
#-g
MKLFLAGS=-D __MKL
#sergio CUDAFLAGS= --gpu-architecture sm_30
#changed with sm_35
CUDAFLAGS= -arch=sm_35
LINKERFLAGS= -Wl,--start-group $(MKLROOT)/lib/intel64/libmkl_intel_lp64.a $(MKLROOT)/lib/intel64/libmkl_sequential.a $(MKLROOT)/lib/intel64/libmkl_core.a $(MKLROOT)/../compiler/lib/intel64/libiomp5.a -Wl,--end-group
####################
#utilities
####################
io.o : io.c
icc $(CFLAGS) -c -O3 io.c -o $(BUILD_DIR)/io.o
#BLAS and LAPACK wrapper
util.o : util.c
icc $(CFLAGS) $(MKLFLAGS) $(BLAS_INCLUDE_DIR) -c -O3 util.c -o $(BUILD_DIR)/util.o
#changed with rdec and -lcudadevrt:
metodos.o : metodos.cu
nvcc $(CUDAFLAGS) $(CUDA_INCLUDE_DIR) -c -O3 -rdc=true metodos.cu -lcudadevrt -o $(BUILD_DIR)/metodos.o
##################################
# PCA files
##################################
#changed with rdec and -lcudadevrt:
GPUutil.o: GPUutil.cu
nvcc $(CUDAFLAGS) $(CUDA_INCLUDE_DIR) -c -O3 -rdc=true GPUutil.cu -lcudadevrt -o $(BUILD_DIR)/GPUutil.o
#changed with rdec and -lcudadevrt:
PCA_Kernels.o: PCA_Kernels.cu
nvcc $(CUDAFLAGS) $(CUDA_INCLUDE_DIR) -c -O3 -rdc=true PCA_Kernels.cu -lcudadevrt -o $(BUILD_DIR)/PCA_Kernels.o
#changed with rdec and -lcudadevrt:
DivideParalelo.o: DivideParalelo.cu
nvcc $(CUDAFLAGS) $(CUDA_INCLUDE_DIR) -c -O3 -rdc=true DivideParalelo.cu -lcudadevrt -o $(BUILD_DIR)/DivideParalelo.o
#everything is already compiled, this is just a call to the linker
DivideParalelo: io.o util.o metodos.o GPUutil.o PCA_Kernels.o DivideParalelo.o
icc $(CFLAGS) $(BUILD_DIR)/io.o $(BUILD_DIR)/util.o $(BUILD_DIR)/metodos.o $(BUILD_DIR)/GPUutil.o $(BUILD_DIR)/PCA_Kernels.o $(BUILD_DIR)/DivideParalelo.o $(CUDA_LIB_DIR) $(BLAS_LIB_DIR) $(LINKERFLAGS) $(utilS) $(CUDALIBS) -o DivideParalelo
####################
#misc
####################
clean:
rm -rf $(BUILD_DIR)/*.o ./DivideParalelo
Any suggestion will be greatly appreciated. Perhaps I misunderstood the separate compilation for dynamic parallelism.
I have solved the problem changing both compilation and linking of each cu file.
Makefile:
MKL =1
#initial definitions (library paths et al.)
CUDA_PATH=/usr/local/cuda-6.5
MKLROOT=/home/emartel/intel/composer_xe_2015.0.090/mkl
BUILD_DIR=./build
####################
#includes
####################
#Cuda includes
CUDA_INCLUDE_DIR=-I. -I$(CUDA_PATH)/include
#BLAS includes
BLAS_INCLUDE_DIR=-I. -I$(MKLROOT)/include
####################
#library search paths
####################
CUDA_LIB_DIR=-L$(CUDA_PATH)/lib64
BLAS_LIB_DIR=-L$(MKLROOT)/lib/intel64 -L$(MKLROOT)/../compiler/lib/intel64
####################
#libraries
####################
CUDALIBS=-lcublas -lcudart
utilS= -lpthread -lm
####################
#other compilation flags
####################
CFLAGS= -Wwrite-strings
MKLFLAGS=-D __MKL
CUDAFLAGS= -arch=sm_35
LINKERFLAGS= -Wl,--start-group $(MKLROOT)/lib/intel64/libmkl_intel_lp64.a $(MKLROOT)/lib/intel64/libmkl_sequential.a $(MKLROOT)/lib/intel64/libmkl_core.a $(MKLROOT)/../compiler/lib/intel64/libiomp5.a -Wl,--end-group
####################
#utilities
####################
io.o : io.c
icc $(CFLAGS) -c -O3 io.c -o $(BUILD_DIR)/io.o
#BLAS and LAPACK wrapper
util.o : util.c
icc $(CFLAGS) $(MKLFLAGS) $(BLAS_INCLUDE_DIR) -c -O3 util.c -o $(BUILD_DIR)/util.o
metodos.o : metodos.cu
nvcc $(CUDAFLAGS) $(CUDA_INCLUDE_DIR) -c -O3 -dc metodos.cu -o $(BUILD_DIR)/metodos.o
##################################
# PCA files
##################################
GPUutil.o: GPUutil.cu
nvcc $(CUDAFLAGS) $(CUDA_INCLUDE_DIR) -c -O3 -dc GPUutil.cu -o $(BUILD_DIR)/GPUutil.o
PCA_Kernels.o: PCA_Kernels.cu
nvcc $(CUDAFLAGS) $(CUDA_INCLUDE_DIR) -c -O3 -dc PCA_Kernels.cu -o $(BUILD_DIR)/PCA_Kernels.o
DivideParalelo.o: DivideParalelo.cu
nvcc $(CUDAFLAGS) $(CUDA_INCLUDE_DIR) -c -O3 -dc DivideParalelo.cu -o $(BUILD_DIR)/DivideParalelo.o
DivideParalelo: io.o util.o metodos.o GPUutil.o PCA_Kernels.o DivideParalelo.o
nvcc $(CUDAFLAGS) $(CUDA_INCLUDE_DIR) -dlink $(BUILD_DIR)/io.o $(BUILD_DIR)/util.o $(BUILD_DIR)/metodos.o $(BUILD_DIR)/GPUutil.o $(BUILD_DIR)/PCA_Kernels.o $(BUILD_DIR)/DivideParalelo.o -lcudadevrt -o $(BUILD_DIR)/link.o
icc $(CFLAGS) $(BUILD_DIR)/io.o $(BUILD_DIR)/util.o $(BUILD_DIR)/metodos.o $(BUILD_DIR)/GPUutil.o $(BUILD_DIR)/PCA_Kernels.o $(BUILD_DIR)/DivideParalelo.o $(BUILD_DIR)/link.o -lcudadevrt $(CUDA_LIB_DIR) $(BLAS_LIB_DIR) $(LINKERFLAGS) $(utilS) $(CUDALIBS) -o DivideParalelo -lcudart
####################
#misc
####################
clean:
rm -rf $(BUILD_DIR)/*.o ./DivideParalelo

MySql connection threw c++ in ubuntu

I have installed necessary packages
sudo apt-get install mysql-server
sudo apt-get install libmysqlcppconn-dev
here is my code:
/* Standard C++ includes */
#include <stdlib.h>
#include <iostream>
/*
Include directly the different
headers from cppconn/ and mysql_driver.h + mysql_util.h
(and mysql_connection.h). This will reduce your build time!
*/
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
using namespace std;
int main(void)
{
cout << endl;
cout << "Running 'SELECT 'Hello World!' AS _message'..." << endl;
try {
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:11840", "root", "n");
/* Connect to the MySQL test database */
con->setSchema("test");
stmt = con->createStatement();
res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); // replace with your statement
while (res->next()) {
cout << "\t... MySQL replies: ";
/* Access column data by alias or column name */
cout << res->getString("_message") << endl;
cout << "\t... MySQL says it again: ";
/* Access column fata by numeric offset, 1 is the first column */
cout << res->getString(1) << endl;
}``
delete res;
delete stmt;
delete con;
} catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
//cout << "(" << __FUNCTION__ << ") on line " ยป
// << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
cout << endl;
return EXIT_SUCCESS;
}
I compiled using:
sudo g++ -Wall -I/usr/include/cppconn -o testapp mysql_connect1.cpp -L/usr/lib -lmysqlcppconn
it got compiled successfully
when I run:
./testapp
Running 'SELECT 'Hello World!' AS _message'...
# ERR: SQLException in mysql_connect1.cpp# ERR:
Can't connect to MySQL server on '127.0.0.1' (111)
(MySQL error code: 2003, SQLState: HY000 )
I get the above error
I made the below changes as suggested here:
run the command vim /etc/mysql/my.cnf
comment bind-address = 127.0.0.1 using the # symbol
restart your mysql server once.
But it still didn't work
output of:
root#knils-HP:/home/knils# sudo netstat -tap | grep mysql
tcp 0 0 *:mysql *:* LISTEN 11840/mysqld
root#knils-HP:/home/knils#
Please can someone help me with this?
Are you sure your server runs on 11840 and the user account you are using is allowed to connect from localhost?
con = driver->connect("tcp://127.0.0.1:11840", "root", "n");
Your console output that more looked like a processID or internal port for me.
MySQL usually runs on 3306 except you changed that on your own.
Can you try:
con = driver->connect("tcp://127.0.0.1:3306", "root", "n");
?