LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   compiling c programs on Linux (https://www.linuxquestions.org/questions/linux-newbie-8/compiling-c-programs-on-linux-731826/)

Mihir V Patankar 06-09-2009 11:08 PM

compiling c programs on Linux
 
Hi

I recently copied a few .so files using ftp from a sun sparc machine on to my linux machine (i686 i686 i386 GNU/Linux).

When i tried to reload the radius application residing on the linux machine i get the following error:-

[ //localhost ]
LicenseInfo = AR-Standard 4.0
AR-Prepaid 4.0
AR-CPU 4.0
Radius/
Administrators/

--> reload

Reloading Server 'Radius'...
310 Command failed
Server 'Radius' failed to start
The server wrote the following errors to the log file:
Configuration Unable to load shared library '/opt/CSCOar/scripts/radius/rex/librexcisco.so' specified by the script /Radius/Scripts/CiscoOutgoingScript. The error message is: /opt/CSCOar/scripts/radius/rex/librexcisco.so: ELF file data encoding not little-endian

I started to get this error after i copied those .so files

Will i need to compile the c programs on linux indepedently?

If that is the case then how do i go about compiling the c programs on my linux machine?

Thanks!

Mihir V Patankar

Uncle_Theodore 06-09-2009 11:15 PM

Well, Solaris libraries are not going to work under Linux, it's a completely different operating system.
Your best bet to install new software under Linux is to download a package designed for your distribution from a repository and use a package manager to install it.
If you definitely want to compile from source, you download the source and read the instructions included by the programmers on how to build their software. Usually, you just unpack the source and run
./configure
make
su
make install
but for different programs things may vary.You'll need a compiler, gcc and some other things. To install them, you again need to get them from you distribution repository or from the installation media.

i92guboj 06-10-2009 12:02 AM

Quote:

Originally Posted by Uncle_Theodore (Post 3568712)
Well, Solaris libraries are not going to work under Linux, it's a completely different operating system.

Not only that, but sparc is also a completely different animal than x86. Not only the two OSes are incompatible at binary level, but they have also been compiled to run on different hardware. Even if he was running linux on the sparc machine, those binaries wouldn't ever work on the x86 one.



Quote:

Originally Posted by Mihir V Patankar (Post 3568705)
Hi

I recently copied a few .so files using ftp from a sun sparc machine on to my linux machine (i686 i686 i386 GNU/Linux).

Why? What are those .so files? Explain a bit what you are trying to do. Are you overwriting already existing linux .so files with the sun sparc ones? Are those .so files maybe plugins or extensions of any kind for radius?

For what I can deduct from the output, you already have radius running on linux, don't you? In that case, what what are you trying to add or change?

Mihir V Patankar 06-16-2009 01:54 AM

Hi,
I copied the following c program from our sun sparc machine to the linux machine (i686 i686 i386 GNU/Linux).I want to compile and run the following c script on the Linux machine.I want to know the steps for compliling and running the c program

#include <string.h>
#include <stdio.h>
#include <fnmatch.h>
#include <stdlib.h>
#include <ctype.h>
#include "rex.h"
#include <time.h>
#include <sys/timeb.h>
#include <arpa/inet.h>
#include "bitstring.h"
#include <mysql.h>


int REXAPI JustTesting( int iScriptingPoint,
rex_AttributeDictionary_t* pRequest,
rex_AttributeDictionary_t* pResponse,
rex_EnvironmentDictionary_t* pEnviron )
{
const char* framedip;

if( strcmp( pEnviron->get( pEnviron, "Request-Type" ),
"Access-Request" ) != 0 &&
strcmp( pEnviron->get( pEnviron, "Request-Type" ),
"Accounting-Request" ) != 0)
{
pEnviron->trace( pEnviron, 10, "ExecRealmRule exiting because packet "
"is not the right request type" );
return REX_OK;
}

if(!(pRequest->containsKey( pRequest, "Framed-IP-Address" ))){
pEnviron->trace( pEnviron, 10, "No Framed IP");
return REX_ERROR;
}else{
framedip = pRequest->get( pRequest, "Framed-IP-Address", 0, 0 );
pEnviron->trace( pEnviron, 10, "Framed IP is %s",framedip);
}

/* cbUserName = pszUserName ? strlen( pszUserName ) : 0; */
pEnviron->trace( pEnviron, 10, "IPaddress is: %s", framedip);
/* Mysql starts here */
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *queryfinal;
char *location_name, *location_id;

char *server = "127.0.0.1";
char *user = "root";
char *password = "*****";
char *database = "mihir";
/* char *querystr = "SELECT * FROM location WHERE ipaddress = \"10.10.10.23\""; */
char *querystr = "SELECT * FROM location WHERE ipaddress = \"";
pEnviron->trace( pEnviron, 10, "STRCATING %s and %s",querystr,framedip);
queryfinal = (char *)calloc(strlen(querystr) + strlen(framedip) + 2, sizeof(char));
strcat (queryfinal,querystr);
strcat (queryfinal,framedip);
strcat (queryfinal,"\"");
pEnviron->trace( pEnviron, 10, "MYSQL INIT TRY....");
conn = mysql_init(NULL);
mysql_real_connect(conn, server,user, password, database, 0, NULL, 0);
pEnviron->trace( pEnviron, 10, "Connected to MySQL");
pEnviron->trace( pEnviron, 10, "Trying query-> %s",queryfinal);
if(mysql_query(conn, queryfinal)){
pEnviron->trace( pEnviron, 10, "Query Failed");
}else{
pEnviron->trace( pEnviron, 10, "Query OK");
}
res = mysql_store_result(conn);
if(mysql_num_rows(res) > 0){
pEnviron->trace( pEnviron, 10, "Record Found");
}else{
pEnviron->trace( pEnviron, 10, "No Record Found");
return REX_ERROR;
}
pEnviron->trace( pEnviron, 10, "Exec query-> %s",queryfinal);
pEnviron->trace( pEnviron, 10, "Query Result Is %d",res);
while ((row = mysql_fetch_row(res)) != NULL){
pEnviron->trace( pEnviron, 10, "Output -> %s %s %s\n", row[0], row[1], row[2]);
pEnviron->trace( pEnviron, 10, "Trying calloc");
location_name = (char *)calloc(strlen(row[1]),sizeof(char));
location_id = (char *)calloc(strlen(row[2]),sizeof(char));
pEnviron->trace( pEnviron, 10, "Trying strcpy");
strcpy (location_name,row[1]);
strcpy (location_id,row[2]);
}

pEnviron->trace( pEnviron, 10, "LocationID: %s, LocationName: %s", location_id,location_name);
pRequest->put( pRequest, "WISPr-Location-Name",location_name , REX_REPLACE );
pRequest->put( pRequest, "WISPr-Location-Id",location_id , REX_REPLACE );


/* Mysql ENDs here */
return REX_OK;


Thanks!

Mihir V Patankar

Mentalikryst 06-16-2009 02:13 AM

To compile your first program, I recommend a Hello World. Simple, guaranteed to work with GCC and takes 2 minutes.

Code:

#include <iostream>

int main()
{
  std::cout << "Hello World!";
  return 0;
}

Now open up a terminal and type:

Code:

gcc -i hello.c -o hello
For multiple file projects, put all of the files into a single folder and then install qmake and then go through this process to build the application:

Code:

cd <PROJECT DIRECTORY>
qmake -project
qmake
make


i92guboj 06-16-2009 03:47 AM

Mihir V Patankar, I have no idea what the code is about, haven't looked deeply into it, however I warn you that it's not a standalone program so it won't be of any use without the main module whatever it is.

Besides that, these two lines:

Code:

#include "rex.h"
...
#include "bitstring.h"

Mean that this .h files are going to be searched for in the same directory where this source that you pasted lives. Which in turn means that the project has many source files that you haven't even considered. Unless you have all the pieces you are not going to be able to build this thing.

However, at least in linux, glibc has a rex.h, so maybe you could change the rex.h line with this one:

Code:

#include <rpcsvc/rex.h>
Can't seem to remember about bitstring.h right now and maybe it's something specific to other *nixes.



Being that said, to compile a simple program in C you would do

Code:

gcc -o foo.o foo.c
When it's not meant to be a standalone program (this is the case) you only want to compile, and not link.

Code:

gcc -o foo.o -c foo.c
But for anything that's something more than a simple c file with a .h you should use a Makefile and the make tool (well, or qmake, cmake or whatever you prefer for that concrete case).

Mentalikryst, you are threating it as C, but what you wrote is C++. I am not going into the debate about why it is a very bad thing to do so, there's already enough of that on the net. I just write this here so any newcomer that sees that knows that, well, it's not technically correct. Don't use .c for c++ files, and compile them using g++. AmbigUity is never a good thing when programming. Besides that, the file that the other guy posted is C.


All times are GMT -5. The time now is 12:30 PM.