How to fix architecture x86_64 errors? - c++

I'm running omnet++ 4.6 for around 6months now. When I tried building my project after a few changes:
removing a header file from the project
adding more files to my includes folder
I get this error
Creating shared library: ../out/gcc-debug/src/libinet.dylib
Undefined symbols for architecture x86_64:
"BloomFilter::BloomFilter(unsigned long, int, unsigned long, ...)", referenced from:
AODVRouting::AODVRouting() in AODVRouting.o
AODVRouting::AODVRouting() in AODVRouting.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[1]: *** [../out/gcc-debug/src/libinet.dylib] Error 1
make: *** [all] Error 2
My project used to build and run fine before this.
This is .cc file:
#include "BloomFilter.h"
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <cstdarg>
#include <exception>
#include <stdio.h>
#include <string.h>
using namespace std;
#define SETBIT(a, n) (a[n/CHAR_BIT] |= (1<<(n%CHAR_BIT)))
#define GETBIT(a, n) (a[n/CHAR_BIT] & (1<<(n%CHAR_BIT)))
// The Constructor
BloomFilter::BloomFilter(size_t size,int hash_k, size_t nfuncs, ...) {
va_list l;
unsigned int n;
try {
this->a=new char[(size+CHAR_BIT-1)/CHAR_BIT];
}
catch(const std::exception& e)
{
// If we get here is that there is an allocation error .
// We must free the memory .
delete(this);
// std :: cerr << "ERROR: " << e.what () << endl;
// Then raise the exception to indicate that an error occurred.
throw;
}
try {
this->funcs= new hashfunc_t[nfuncs];
}
catch(const std::exception& e){
delete(this->a);
delete(this);
}
va_start(l, nfuncs);
for(n=0; n < nfuncs; ++n) {
this->funcs[n]=va_arg(l, hashfunc_t);
}
va_end(l);
this->nfuncs=nfuncs;
this->asize=size;
this->hash_k=hash_k;
}
// The Destructor
BloomFilter::~BloomFilter() {
/*
delete(this->a);
delete(this->funcs);
delete(this);
*/
}
int BloomFilter::AddToBloom(std::string word){
char t= '1';
int AddFlag; // to know if the element is added successfully
for(int i=0;i<this->hash_k;i++){
AddFlag=Add(word += t);
t++;
}
return AddFlag;
}
int BloomFilter::Add(std::string word){
size_t size = word.size() + 1;
char * buffer = new char[ size ];
strncpy( buffer, word.c_str(), size );
return Add(buffer);
}
int BloomFilter::Add(const char *s)
{
size_t n;
for(n=0; n<this->nfuncs; ++n) {
SETBIT(this->a, this->funcs[n](s)%this->asize);
}
return 0;
}
int BloomFilter::CheckBloom( std::string word){
int CheckFlag;
char t= '1';
for(int i=0;i<this->hash_k;i++){
if(!Check(word += t)) return 0;
t++;
}
return 1;
}
int BloomFilter::Check(std::string word){
size_t size = word.size() + 1;
char * buffer = new char[ size ];
strncpy( buffer, word.c_str(), size );
return Check(buffer);
}
int BloomFilter::Check(const char *s)
{
size_t n;
for(n=0; n< this->nfuncs; ++n) {
if(!(GETBIT(this->a, this->funcs[n](s)%this->asize))) return 0;
}
return 1;
}
//Print information about this object
void BloomFilter::toString(){
/*EV << "[BloomFilter] Hello, I am ready ? " << ready
<<" ; max entry :" << maxEntry << endl;*/
}
What's the fix for this error?

That's a linker, not a C++ compiler error. You will probably have to add the .o file generated from you .cc file to the list of objects that get linked together to form libinet.dylib

I got rid of this error after copying files .cc and .h from my root project to the project I'm working on (in the specified directory).
I did this while running omnet++ and within the projects browser.
I'm not sure how does that link to the "linker" problem but it definitely solved mine.

Related

C++ Time Library and Octave .oct files

I am trying to write an Octave C++ .oct function that uses the linasm-1.13 library but I cannot seem to get even basic loading of tzdata from /usr/share/zoneinfo/ to work. My simple test function so far is
#include <octave/oct.h>
#include <Time.h> // the linasm-1.13 library
DEFUN_DLD ( tz, args, nargout,
"-*- texinfo -*-\n\
#deftypefn {Function File} {} tz (#var{YYYYMMDDHHMMSS})\n\
\n\
#end deftypefn" )
{
octave_value_list retval_list ;
unsigned int tz ;
const char *ny_time = "/usr/share/zoneinfo/America/New_York" ;
tz = Time::LoadTimeZone( ny_time ) ;
return retval_list ;
which, on compiling with mkoctfile, gives this error
>> mkoctfile tz.cc
tz.cc: In function ‘octave_value_list Ftz(const octave_value_list&, int)’:
tz.cc:24:34: error: cannot call member function ‘unsigned int Time::LoadTimeZone(const char*)’ without object
tz = Time::LoadTimeZone( ny_time ) ;
^
warning: mkoctfile: building exited with failure status
My understanding of this is that ny_time is not an object that is recognised, but I have tried casting ny_time as a string literal as detailed in this accepted SO answer.
I am doing things this way because the input for LoadTimeZone according to the linasm page should be a "path to tzfile, which describes required time zone." Where am I going wrong?
I think you have to #include "source.cc" files also, not just the #include "header.h" files. In your case, I guess you should add: #include "Time.cc" or something like that. I don't know why but this worked for me when working with Rafat's Hussain wavemin library, but I had only 4 files, it must be incredibly tedious with lots of files.
This is what I did (it's a modified version of the test code provided by Rafat with his library).
#include "wavemin.h"
#include "waveaux.h"
#include "wavemin.cc"
#include "waveaux.cc"
#include <octave/oct.h>
double ensayo();
double absmax(double *array, int N);
DEFUN_DLD(helloctave2, argv, , "Usage: hello()"){
wave_object obj;
wt_object wt;
double *inp, *out, *diff;
int N, i, J;
char *name = "db4";
obj = wave_init(name);// Initialize the wavelet
N = 14; //Length of Signal
inp = (double*)malloc(sizeof(double)* N); //Input signal
out = (double*)malloc(sizeof(double)* N);
diff = (double*)malloc(sizeof(double)* N);
//wmean = mean(temp, N);
for (i = 0; i < N; ++i) {
inp[i] = i;
}
J = 1; //Decomposition Levels
wt = wt_init(obj, "dwt", N, J);// Initialize the wavelet transform object
setDWTExtension(wt, "sym");// Options are "per" and "sym". Symmetric is the default option
setWTConv(wt, "direct");
dwt(wt, inp);// Perform DWT
//DWT output can be accessed using wt->output vector. Use wt_summary to find out how to extract appx and detail coefficients
for (i = 0; i < wt->outlength; ++i) {
octave_stdout << wt->output[i];
octave_stdout << "\n";
}
idwt(wt, out);// Perform IDWT (if needed)
// Test Reconstruction
for (i = 0; i < wt->siglength; ++i) {
diff[i] = out[i] - inp[i];
}
octave_stdout << absmax(diff, wt->siglength);
octave_stdout << "\n";
octave_value_list retval;
return retval;
}
double
absmax(double *array, int N) {
double max;
int i;
max = 0.0;
for (i = 0; i < N; ++i) {
if (fabs(array[i]) >= max) {
max = fabs(array[i]);
}
}
return max;
}

Error LNK2005 in C++ and ifndef don't work

I have a problem with Visual Studio 2012 & 2015 about the fact than it's seem than the "ifndef" don't work. I use the "ifndef" for "NAN" and "ifndef" for the header file and it's said these 2 errors (see the image). When I add the link "#include"Outil.h"" in the header of other file, I see the same message of error.
I always do like this before and it's always work. I don’t understand why it's doesn't work now even with only two files.
I also try to change the name of the first function "realoc_ungraded" but it's doesn't work and I get the same error.
Message of error
The message:
1) Warning: C4005: 'NAN': macro redefinition of math.h
2) Error: LNK2005: "struct tab_dynamo __cdecl realoc_ugraded(struct tab_dynamo,unsigned int)" (?realoc_ugraded##YA?AUtab_dynamo##U1#I#Z) already defined in main.obj Project1
3) Error: LNK1169: one or more multiply defined symbols found Projet
There is the code of the different file:
File main.cpp
#include"Outil.h"
int main(void) {
return 0;
}
File Outil.h
#ifndef LIBRARY_OF_TOOLS
#define LIBRARY_OF_TOOLS 0
#define _USE_MATH_DEFINES
//NAN not defined in Visual Studio 2012, so I use the def. of VS 2015
#ifndef NAN
#define NAN ((float)(std::numeric_limits<float>::infinity*0.0F))
#endif
#include<iostream>
#include<string>
using namespace std;
#include<cmath>
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>
#define ERROR_ADRESSE 0xcccccccc //default when not initialised
#define DEFAULT_LENGHT_TAB 1
//-----------------------------------------
typedef double type_data; //the type for calculation
//-----------------------------------------
/*Struct for my array*/
typedef struct {
type_data *tab;
unsigned int length;
}tab_dynamo;
//-----------------------------------------
template<typename T>
bool verify_ptr(const T *ptr) {
return (ptr == NULL || ptr == (T*)(ERROR_ADRESSE));
}
//-----------------------------------------
template<typename T>
void see_tab(const T *tab, const unsigned int taille) {
unsigned int i;
cout << endl << endl;
if (verify_ptr(tab) == false && taille > 0) {
cout << endl;
for (i = 0; i<taille; ++i) {
cout << tab[i] << "\t";
}
}
cout << endl << endl;
}
//-----------------------------------------
template<typename T>
T* realoc_ungraded(const T* original_tab, unsigned int *length, const unsigned int new_length) {
T* new_tab = NULL;
unsigned int precedent_length = 0, i;
/*1) Exception case to directly exit*/
if (new_length == 0) {
return NULL;
}
/*2) Verification of the ptr of the length*/
if (verify_ptr(length)) {
length = (unsigned int*)calloc(1, sizeof(unsigned int));
assert(length);
}
precedent_length = *length;
*length = new_length;
/*4) Creation of the new tab.*/
new_tab = (T*)calloc(*length, sizeof(T));
assert(new_tab);
/*5) To transfert data of the original tab to the new tab*/
if (precedent_length != 0 && verify_ptr(original_tab) == false) {
for (i = 0; i < precedent_length && i < new_length; ++i) {
new_tab[i] = original_tab[i];
}
}
return new_tab;
}
//-----------------------------------------
//Version with the use of the struct "tab_dynamo"
tab_dynamo realoc_ungraded(tab_dynamo original_tab, const unsigned int new_length) {
tab_dynamo tableau = { NULL, 0 };
tableau.tab = realoc_ugraded(original_tab.tab, &original_tab.length, new_length);
tableau.length = new_length;
return tableau;
}
#endif
File Outil.cpp:
#include"Outil.h"
#ifndef NAN
#define NAN ((float)(std::numeric_limits<float>::infinity*0.0F))
#endif
When preprocessor process these, the NAN is defined because it's not defined yet.
#include<cmath>
Then cmath maybe include math.h, and found NAN is defined by yours.
You can try to change the sequence of include and your definition.
#include <cmath>
#ifndef NAN
#define NAN ((float)(std::numeric_limits<float>::infinity*0.0F))
#endif
B.T.W If you compile using gcc, you could use -E option to see the output of preprocessor and know how the preprocess expand the macros.

C++ Dynamic Array: A value of type "void" cannot be used to initialize an entity of type "int"

I am working on a C++ project for school in which the program will read in a list of numbers from a text file, store them in a dynamic array, then print them out to another text file. To be honest I'm a little lost with the pointers in this, and I am getting the error "A value of type "void" cannot be used to initialize an entity of type "int"" in my main source file.
Main.cpp (this is where I'm getting the error):
#include "dynamic.h"
int main
{
readDynamicData("input.txt","output.txt");
}
dynamic.cpp (the skeleton for the program):
#include "dynamic.h"
void readDynamicData(string input, string output)
{
DynamicArray da; //struct in the header file
da.count = 0;
da.size = 5; //initial array size of 5
int *temp = da.theArray;
da.theArray = new int[da.size];
ifstream in(input);
ofstream out(output);
in >> da.number; //prime read
while (!in.fail())
{
if (da.count < da.size)
{
da.theArray[da.count] = da.number;
da.count++;
in >> da.number; //reprime
}
else grow; //if there are more numbers than the array size, grow the array
}
out << "Size: " << da.size << endl;
out << "Count: " << da.count << endl;
out << "Data:" << endl;
for (int i = 0; i < da.size; i++)
out << da.theArray[i];
in.close();
out.close();
delete[] temp;
}
void grow(DynamicArray &da) //this portion was given to us
{
int *temp = da.theArray;
da.theArray = new int[da.size * 2];
for (int i = 0; i<da.size; i++)
da.theArray[i] = temp[i];
delete[] temp;
da.size = da.size * 2;
}
and dynamic.h, the header file:
#include <iostream>
#include <string>
#include <fstream>
#ifndef _DynamicArray_
#define _DynamicArray_
using namespace std;
void readDynamicData(string input, string output);
struct DynamicArray
{
int *theArray;
int count;
int size;
int number;
};
void grow(DynamicArray &da);
#endif
you have to add parenthesis to main or any function:
int main(){/*your code here ...*/};
2- you are using an unitialized objct:
DynamicArray da; //struct in the header file
da.count = 0;
da.size = 5; //initial array size of 5
so int* theArray is a member data and is uninitialized so welcome to a segfault
all the members of da are not initialized so you have to do before using it.
3- also you add parenthesis to grow function:
else grow(/*some parameter here*/); // grow is a function
4- using namespace std; in a header file is a very bad practice.
tip use it inside source
5- why making inclusion of iostream and string.. before the inclusion guard??
correct it to:
#ifndef _DynamicArray_
#define _DynamicArray_
#include <iostream>
#include <string>
#include <fstream>
/*your code here*/
#endif
main is a function so it needs brackets:
int main(){
// your code
return 0; // because it should return intiger
}
And. Your grow is also a function, so if you want to call it you write grow() and it needs DynamicArray as a parameter.
It is impossible to write working programs on C/C++ any programming language not knowing a basic syntax.

Error when compiling the code, linker command failed?

I am relatively new to C++ and I am using CLions. I am trying to run this code as follows:
/*
* File: Warmup.cpp
* ----------------
#include <iostream>
#include <string>
#include "../lib/StanfordCPPLib/console.h"
#include "../lib/StanfordCPPLib/simpio.h"
using namespace std;
/* Constants */
const int HASH_SEED = 5381; /* Starting point for first cycle */
const int HASH_MULTIPLIER = 33; /* Multiplier for each cycle */
const int HASH_MASK = unsigned(-1) >> 1; /* All 1 bits except the sign */
/* Function prototypes */
int hashCode(string key);
/* Main program to test the hash function */
int main() {
string name = getLine("Please enter your name: John");
int code = hashCode(name);
cout << "The hash code for your name is " << code << "." << endl;
return 0;
}
/*
* Function: hash
* Usage: int code = hashCode(key);
* --------------------------------
int hashCode(string str) {
unsigned hash = HASH_SEED;
int nchars = str.length();
for (int i = 0; i < nchars; i++) {
hash = HASH_MULTIPLIER * hash + str[i];
}
return (hash & HASH_MASK);
}
However, I am getting the following errors:
[ 50%] Building CXX object CMakeFiles/Warmup.dir/src/Warmup.cpp.o
[100%] Linking CXX executable Warmup ld: library not found for
-llib/StanfordCPPLib clang: error: linker command failed with exit code 1 (use -v to see invocation) make[3]: * [Warmup] Error 1
make[2]: [CMakeFiles/Warmup.dir/all] Error 2 make[1]:
[CMakeFiles/Warmup.dir/rule] Error 2 make: * [Warmup] Error 2
I know that this works for microsoft studio C++ but I am not sure why it isnt running for CLions. Would someone mind giving some advice here ?
Any help would be greatly appreciated.
Thanks
Edit: This is what my current CMakeLists.txt file looks like:
cmake_minimum_required(VERSION 3.6)
project(0_Warmup)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILE src/Warmup.cpp)
link_libraries(lib/StanfordCPPLib)
add_executable(Warmup src/Warmup.cpp)
Am I making an error linking this library somewhere ?
I think u added above this //
#include "../lib/StanfordCPPLib/console.h"
#include "../lib/StanfordCPPLib/simpio.h"// library and u shud try once again a right path of library folder ..

Compiler error undefined symbol

I have simple class ina header file:
> cat Algorithms.hh
#ifndef Algorithms_hh
#define Algorithms_hh
#include<vector>
class Algorithms
{
public:
Algorithms();
void BubbleSort();
std::vector<int> myarray;
};
#endif
Then a corresponding c file:
> cat Algorithms.cc
#include <iostream>
#include <vector>
#include "Algorithms.hh"
Algorithms::Algorithms()
{
myarray.push_back(0);
}
void Algorithms::BubbleSort()
{
int i, j, flag = 1; // set flag to 1 to start first pass
int temp; // holding variable
int numLength = myarray.size();
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (j=0; j < (numLength -1); j++)
{
if (myarray[j+1] > myarray[j]) // ascending order simply changes to <
{
temp = myarray[j]; // swap elements
myarray[j] = myarray[j+1];
myarray[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
}
>
And then the main function:
> cat algo2.cc
#include <iostream>
#include <vector>
#include "Algorithms.hh"
using namespace std;
int main(int argc,char **argv)
{
Algorithms *arr=new Algorithms();
arr->myarray.push_back(1);
arr->myarray.push_back(2);
arr->myarray.push_back(100);
return 0;
}
>
When i compile the main:
I get the below error:
> CC algo2.cc
Undefined first referenced
symbol in file
Algorithms::Algorithms() algo2.o
ld: fatal: Symbol referencing errors. No output written to a.out
Can anyone tell me where i am wrong?
This is a linker error, the linker is telling you it can't find the definition of constructor of class Algorithms. You should compile with:
CC Algorithms.cc algo2.cc
You can identify it's a linker error because of the ld: in front of the error.
And of course as stated by Kerrek SB you need to declare your constructor without the Algorithms:: in front of it...
You've just forgotten to include both .cc files into compiling:
cc algo2.cc Algorithms.cc
If you include header file with declarations, like
#include "Algorithms.hh"
you should also provide implementation, definition in .c, or .lib. or load library with definition dynamically. In your case your library is Algorithms.cc, so just add it into compilation stage, and then both temporary object files
Algo2.a + Algorithms.a
will go to
a.out