c code make successfully in Mac while failed in Linux - linux

I have some source files(e.g, layout.cpp). I can use 'make' command in my local MAC machine. After make successfully, I copied all files to remote Linux machine. However, the executable file in my MAC machine could not be executed in remote Linux machine. The error message is below.
layout is not a binary executable file
I think the failure is due to format of layout file is 'Mach-O 64-bit executable', which couldn't run in linux machine.
Therefore, I tried to make source files in remote linux machine. However, it showed a lot of error messages like below.
layout.cpp:75: error: ‘exit’ was not declared in this scope
layout.cpp:88: error: ‘strcpy’ was not declared in this scope
But these errors didn't show in make process in MAC. Are these errors caused by difference of compilers in MAC and Linux? Since there are so many different errors, hence we could not simply add '#include cstdlib' or '#include string.h' to solve that. Thanks.
Source Code:
#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <string>
#include <deque>
#include <vector>
using namespace std;
// layout routines and constants
#include <layout.h>
#include <parse.h>
#include <graph.h>
// MPI
#ifdef MUSE_MPI
#include <mpi.h>
#endif
int main(int argc, char **argv) {
// initialize MPI
int myid, num_procs;
#ifdef MUSE_MPI
MPI_Init ( &argc, &argv );
MPI_Comm_size ( MPI_COMM_WORLD, &num_procs );
MPI_Comm_rank ( MPI_COMM_WORLD, &myid );
#else
myid = 0;
num_procs = 1;
#endif
// parameters that must be broadcast to all processors
int rand_seed;
float edge_cut;
char int_file[MAX_FILE_NAME];
char coord_file[MAX_FILE_NAME];
char real_file[MAX_FILE_NAME];
char parms_file[MAX_FILE_NAME];
int int_out = 0;
int edges_out = 0;
int parms_in = 0;
float real_in = -1.0;
// user interaction is handled by processor 0
if ( myid == 0 )
{
if ( num_procs > MAX_PROCS )
{
cout << "Error: Maximum number of processors is " << MAX_PROCS << "." << endl;
cout << "Adjust compile time parameter." << endl;
#ifdef MUSE_MPI
MPI_Abort ( MPI_COMM_WORLD, 1 );
#else
exit (1);
#endif
}
// get user input
parse command_line ( argc, argv );
rand_seed = command_line.rand_seed;
edge_cut = command_line.edge_cut;
int_out = command_line.int_out;
edges_out = command_line.edges_out;
parms_in = command_line.parms_in;
real_in = command_line.real_in;
strcpy ( coord_file, command_line.coord_file.c_str() );
strcpy ( int_file, command_line.sim_file.c_str() );
strcpy ( real_file, command_line.real_file.c_str() );
strcpy ( parms_file, command_line.parms_file.c_str() );
}
// now we initialize all processors by reading .int file
#ifdef MUSE_MPI
MPI_Bcast ( &int_file, MAX_FILE_NAME, MPI_CHAR, 0, MPI_COMM_WORLD );
#endif
graph neighbors ( myid, num_procs, int_file );
// finally we output file and quit
float tot_energy;
tot_energy = neighbors.get_tot_energy ();
if ( myid == 0 )
{
neighbors.write_coord ( coord_file );
cout << "Total Energy: " << tot_energy << "." << endl
<< "Program terminated successfully." << endl;
}
// MPI finalize
#ifdef MUSE_MPI
MPI_Finalize ();
#endif
}

You are missing several standard #include notably the standard C++ header <cstdlib> or the C header <stdlib.h> (for exit) and the standard C++ header <cstring> or the C header <string.h> (for strcpy), as commented by Jonathan Leffler (who also explained why that works on MacOSX but not on Linux).
You probably should switch to C++11 standard. This means installing a recent GCC (4.8) and compile with g++ -std=c++11 and of course -Wall -g (to get all warnings and debugging information) ....
And you did not search enough on these issues. You could have typed man exit to get exit(3) man page, or man strcpy to get strcpy(3) man page. Both man pages give relevant C include headers....
BTW, the bug is really in your source code. A code using exit really should include <stdlib.h> or <cstdlib> explicitly by itself (at least for readability reasons). You should not suppose that some other system header is (accidentally) including that.

Related

Question about this error: ‘bdget’ was not declared in this scope

trying to figure out why I’m getting this error about ’bdget’ (when on Linux Ubuntu, info below) and how to get rid of this error, maybe you could suggest some specific steps for me to try and/or ask me to provide you some additional info about the error, thank you for any help.
error: ‘bdget’ was not declared in this scope
b_dev = bdget(st.st_rdev);
The code (for a short sample/tester program put together quickly to demonstrate the problem here to you) that produces this error is below, here’s other info: When I comment out that ‘bdget’ line, the program (seen below) compiles, otherwise it doesn’t and produces the error. I understand from other stackoverflow.com questions that using bdget requires #include <linux/fs.h>, which I’ve done here as you can see in the code below.
I think this is my first question here at stackoverflow.com, haven’t yet learned what question-related actions I need to take here at stackoverflow.com about your answers such as accept, and the steps for taking those actions, so bear with me a bit.
Regards,
Yaman Aksu,PhD
The build used this g++ version (full path: /usr/bin/g++) :
g++ (Ubuntu 7.3.0-27ubuntu1-18.04) 7.3.0 <snip>
and was simple as follows:
g++ -Wall -03 tester.cpp -o tester
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/sysmacros.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <linux/fs.h>
#define FNABS_SIZE 1024
char prog[FNABS_SIZE];
using namespace std;
int main(int argc, char *argv[])
{
int rv=0;
struct dirent *de;
struct stat st;
struct block_device *b_dev;
char direc[FNABS_SIZE], fnabs[FNABS_SIZE], fnabs_bfile[FNABS_SIZE], hd_serial[NAME_MAX], hd_model[NAME_MAX];
int status;
unsigned int maj_rdev, min_rdev;
ios::sync_with_stdio();
strcpy(prog,argv[0]); argc--;
strcpy(direc,"/dev/disk/by-id");
for (DIR *p = opendir(direc); (de=readdir(p)); ) {
if (de->d_type==DT_LNK) {
sprintf(fnabs,"%s/%s",direc,de->d_name);
cout << "[glk] Now running 'stat' on this: " << fnabs << endl;
status = stat(fnabs, &st);
cout << "[glk] 'stat' results:" << endl;
cout << "[glk]\t return value:" << status << endl;
cout << "[glk]\t st.st_rdev:" << st.st_rdev << endl;
cout << "[glk]\t st.st_dev=" << st.st_dev << endl;
cout << "[glk]\t st.st_ino=" << st.st_ino << endl;
maj_rdev = major(st.st_rdev); min_rdev = minor(st.st_rdev);
printf("[glk]\t major:minor = %u:%u\n",maj_rdev, min_rdev);
if ((st.st_mode & S_IFMT) == S_IFBLK) {
printf("[glk]\t This is a 'block device' !!\n");
}
b_dev = bdget(st.st_rdev);
}
}
}

SDL2 Threads C++ pointer corruption

So, i have the following problem which may seem pretty strange or too elementary. This code snippet demonstrates my problem.
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include "SDL2/SDL.h"
#include <iostream>
using namespace std;
int doSTH(void* data){
int* data2 = (int*)data;
cout << data2 << endl;
return 0;
}
int main(){
SDL_Init(SDL_INIT_EVERYTHING);
int* data = new int(2);
cout << data << endl;
SDL_CreateThread(doSTH, "sth", (void*)data);
SDL_Delay(1);
delete data;
SDL_Quit();
}
Output is
0x2479f40
0x400c05
That means that somehow the function i call doesn't get the pointer i give it, am i missing something?
I am using Linux Ubuntu 14.04, g++ 4.8 and codeblocks.
Please tell me if i should give any more info.
Thanks in advance.
Nevermind, somehow the build of SDL2 was screwed up. I just uninstalled libx11-dev, rebooted and then reinstalled libsdl2-dev and now it works correctly.

Using thrust with openmp: no substantial speed up obtained

I am interested in porting a code I had written using mostly the Thrust GPU library to multicore CPU's. Thankfully, the website says that thrust code can be used with threading environments such as OpenMP / Intel TBB.
I wrote a simple code below for sorting a large array to see the speedup using a machine which can support upto 16 Open MP threads.
The timings obtained on this machine for sorting a random array of size 16 million are
STL : 1.47 s
Thrust (16 threads) : 1.21 s
There seems to be barely any speed-up. I would like to know how to get a substantial speed-up for sorting arrays using OpenMP like I do with GPUs.
The code is below (the file sort.cu). Compilation was performed as follows:
nvcc -O2 -o sort sort.cu -Xcompiler -fopenmp -DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_BACKEND_OMP -lgomp
The NVCC version is 5.5
The Thrust library version being used is v1.7.0
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <ctime>
#include <time.h>
#include "thrust/sort.h"
int main(int argc, char *argv[])
{
int N = 16000000;
double* myarr = new double[N];
for (int i = 0; i < N; ++i)
{
myarr[i] = (1.0*rand())/RAND_MAX;
}
std::cout << "-------------\n";
clock_t start,stop;
start=clock();
std::sort(myarr,myarr+N);
stop=clock();
std::cout << "Time taken for sorting the array with STL is " << (stop-start)/(double)CLOCKS_PER_SEC;
//--------------------------------------------
srand(1);
for (int i = 0; i < N; ++i)
{
myarr[i] = (1.0*rand())/RAND_MAX;
//std::cout << myarr[i] << std::endl;
}
start=clock();
thrust::sort(myarr,myarr+N);
stop=clock();
std::cout << "------------------\n";
std::cout << "Time taken for sorting the array with Thrust is " << (stop-start)/(double)CLOCKS_PER_SEC;
return 0;
}
The device backend refers to the behavior of operations performed on a thrust::device_vector or similar reference. Thrust interprets the array/pointer you are passing it as a host pointer, and performs host-based operations on it, which are not affected by the device backend setting.
There are a variety of ways to fix this issue. If you read the device backend documentation you will find general examples and omp-specific examples. You could even specify a different host backend which should have the desired behavior (OMP usage) with your code, I think.
Once you fix this, you'll get an additional result surprise, perhaps: thrust appears to sort the array quickly, but reports a very long execution time. I believe this is due (on linux, anyway) to the clock() function being affected by the number of OMP threads in use.
The following code/sample run has those issues addressed, and seems to give me a ~3x speedup for 4 threads.
$ cat t592.cu
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <ctime>
#include <sys/time.h>
#include <time.h>
#include <thrust/device_ptr.h>
#include <thrust/sort.h>
int main(int argc, char *argv[])
{
int N = 16000000;
double* myarr = new double[N];
for (int i = 0; i < N; ++i)
{
myarr[i] = (1.0*rand())/RAND_MAX;
}
std::cout << "-------------\n";
timeval t1, t2;
gettimeofday(&t1, NULL);
std::sort(myarr,myarr+N);
gettimeofday(&t2, NULL);
float et = (((t2.tv_sec*1000000)+t2.tv_usec)-((t1.tv_sec*1000000)+t1.tv_usec))/float(1000000);
std::cout << "Time taken for sorting the array with STL is " << et << std::endl;;
//--------------------------------------------
srand(1);
for (int i = 0; i < N; ++i)
{
myarr[i] = (1.0*rand())/RAND_MAX;
//std::cout << myarr[i] << std::endl;
}
thrust::device_ptr<double> darr = thrust::device_pointer_cast<double>(myarr);
gettimeofday(&t1, NULL);
thrust::sort(darr,darr+N);
gettimeofday(&t2, NULL);
et = (((t2.tv_sec*1000000)+t2.tv_usec)-((t1.tv_sec*1000000)+t1.tv_usec))/float(1000000);
std::cout << "------------------\n";
std::cout << "Time taken for sorting the array with Thrust is " << et << std::endl ;
return 0;
}
$ nvcc -O2 -o t592 t592.cu -Xcompiler -fopenmp -DTHRUST_DEVICE_SYSTEM=THRUST_DEVICE_BACKEND_OMP -lgomp
$ OMP_NUM_THREADS=4 ./t592
-------------
Time taken for sorting the array with STL is 1.31956
------------------
Time taken for sorting the array with Thrust is 0.468176
$
Your mileage may vary. In particular, you may not see any improvement as you go above 4 threads. There may be a number of factors which prevent an OMP code from scaling beyond a certain number of threads. Sorting generally tends to be a memory-bound algorithm, so you will probably observe an increase until you have saturated the memory subsystem, and then no further increase from additional cores. Depending on your system, it's possible you could be in this situation already, in which case you may not see any improvement from OMP style multithreading.

How to calculate the intersection of a line segment and circle with CGAL

I've been banging my head against a wall trying to understand how to use CGAL's Circular Kernel to calculate the intersection(s) between a line segment (Line_Arc_2) and a Circle (Circle_2). Unfortunately there isn't much in the way of example code for the Circular Kernel, and I'm not finding the reference manual much help.
Here is code that I thought would work, but right now it won't even compile (Mac OS 10.9 using the latest system compiler):
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2)>::type Res;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<Res> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}
I get an error on the result_of line: "error: no type named 'result_type' in...", and a second error that "no viable overloaded '='" is available for the intersection line.
Also, since this would probably be the follow up question once this is working: how do I actually get at the intersection points that are put in the vector? CGAL's documentation suggests to me "result" should contain pairs of a Circular_arc_point_2 and an unsigned int representing its multiplicity. Is this what I will actually get in this case? More generally, does anyone know a good tutorial for using the Circular Kernel and Spherical Kernel intersection routines?
Thanks!
So it seems that result_of doesn't work here, despite being suggested in the CGAL reference manual for the CircularKernel's intersection function.
Here is a different version that seems to work and can properly handle the output:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/iterator.h>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> IsectOutput;
using namespace std;
int main(){
int n = 0;
Circ2 c = Circ2(Pt2(1.0,0.0), Pt2(0.0,1.0), Pt2(-1.0, 0.0));
LineArc2 l = LineArc2( Pt2(0.0,-2.0), Pt2(0.0,2.0) );
std::vector<IsectOutput> output;
typedef CGAL::Dispatch_output_iterator< CGAL::cpp11::tuple<IsectOutput>,
CGAL::cpp0x::tuple< std::back_insert_iterator<std::vector<IsectOutput> > > > Dispatcher;
Dispatcher disp = CGAL::dispatch_output<IsectOutput>( std::back_inserter(output) );
CGAL::intersection(l, c, disp);
cout << output.size() << endl;
for( const auto& v : output ){
cout << "Point: (" << CGAL::to_double( v.first.x() ) << ", " << CGAL::to_double( v.first.y() ) << "), Mult: "
<< v.second << std::endl;
}
return 0;
}
result_of is working but the operator you are asking for does not exist, you are missing the output iterator.
However, I agree the doc is misleading. I'll try to fix it.
The following code is working fine:
#include <vector>
#include <iterator>
#include <CGAL/Exact_circular_kernel_2.h>
#include <CGAL/Circular_kernel_intersections.h>
#include <CGAL/intersections.h>
#include <CGAL/result_of.h>
#include <CGAL/iterator.h>
#include <CGAL/point_generators_2.h>
#include <boost/bind.hpp>
typedef CGAL::Exact_circular_kernel_2 CircK;
typedef CGAL::Point_2<CircK> Pt2;
typedef CGAL::Circle_2<CircK> Circ2;
typedef CGAL::Line_arc_2<CircK> LineArc2;
typedef boost::variant<std::pair<CGAL::Circular_arc_point_2<CircK>, unsigned> > InterRes;
typedef CGAL::cpp11::result_of<CircK::Intersect_2(Circ2,LineArc2,std::back_insert_iterator<std::vector<InterRes> >)>::type Res;
int main(){
Circ2 c = Circ2(Pt2(1,0), Pt2(0,1), Pt2(-1, 0));
LineArc2 l = LineArc2( Pt2(0,-2), Pt2(0,2) );
std::vector<InterRes> result;
CGAL::intersection(c, l, std::back_inserter(result));
return 0;
}

cout in Visual Studio 2010

When I attempt to compile the following
#include <iostream>
using namespace std;
#include "stdafx.h" // This was included by Visual Studio
int _tmain(int argc, _TCHAR* argv[]) // The name _tmain was generated by Visual Studio
{
int a = 1;
cout << a << "\n";
return 0;
}
I get a compiler message:
warning C4627: '#include <iostream>': skipped when looking for precompiled header use
Add directive to 'StdAfx.h' or rebuild precompiled header
Then I'm told that cout is undefined. (It doesn't help to write std::cout.)
I'm using a default Visual Studio projects. This is the first time I've used this. Suggestions appreciated.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a=10;
cout << a << "\n";
cin>>a;
return 0;
}
Slam dear! I have written the same code, but only changing the order of first three line. It gives result at console without any error or warning. Please check it.
Put your iostream include and the std namespace declaration after the stdafx.h include. The program will then compile and run.
As to why, my guess is that precompiled headers (enabled by default) rely on the exact sequence of #include directives. Putting iostream first means that the PCH for stdafx no longer matches the actual sequence of declarations known to the compiler at that point.

Resources