GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Sep-2007, 08:32
Dark dude Dark dude is offline
New Member
 
Join Date: Sep 2007
Posts: 2
Dark dude is on a distinguished road

Help With Static Libraries?


So, basically, I'm writing a test static library + application in Dev-C++ in order to understand how I can get it to function properly, however, despite endless google searching for 6 hours flat, nothing has helped.

The library contains a single function which takes an integer parameter, and returns that integer multiplied by 5. The actual application gets an integer from the user, and passes it to the function in the library, and then prints out the result.

The actual result is the following:


The source is as follows:

Static Library (Sum.a):

main.c:
CPP / C++ / C Code:
#include <stdio.h>

#include "sum.h"

int Sum(int i)
{
    return i * 5;
}

sum.h:
CPP / C++ / C Code:
int Sum(int i);


Test Application [test.exe]:

main.cpp:
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

#include "sum.h"

int main()
{
  int sumnum;
  scanf("%d",sumnum);
  sumnum = Sum(sumnum);
  printf("%d",sumnum);
  system("PAUSE");	
  return 0;
}

My compile logs, incase they're of any help, are as follows:

Sum.a
Quote:
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Projects\Tests\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Projects\Tests\Makefile.win" all
g++.exe -c main.c -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
ar r Sum.a main.o
ar: creating Sum.a
ranlib Sum.a

Execution terminated

Test.exe
Quote:
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Projects\Tests\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Projects\Tests\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
g++.exe main.o -o "test.exe" -L"C:/Dev-Cpp/lib" Sum.a

Execution terminated
Compilation successful

Anybody know what's going on? I've gone through a library worth of information online to no avail (excuse the pun).


Thanks,
~Dark Dude~
  #2  
Old 03-Sep-2007, 10:58
davis
 
Posts: n/a

Re: Help With Static Libraries?


Here is a complete, working example of using a static library.

CPP / C++ / C Code:
// static_main.cpp
#include <iostream>

using namespace std;

#include <static_lib.h>

int main()
{
    int a = 3, b = 4, c = 0;
    c = add( a, b );
    cout << a << " + " << b << " = " << c << endl;
    return 0;
}

CPP / C++ / C Code:
// static_lib.h

#ifdef __cplusplus
extern "C"
{
#endif

int add( int a, int b );

#ifdef __cplusplus
}
#endif

CPP / C++ / C Code:
// static_lib.h

#ifdef __cplusplus
extern "C"
{
#endif

int add( int a, int b );

#ifdef __cplusplus
}
#endif

Code:
# Makefile for creating static libraries CC = g++ CPPFLAGS = -Wall -W -pedantic INC = -I. LIBPATH = . LIBNAME = static_lib OBJS = $(LIBNAME).o LIBOUT = lib$(LIBNAME).a TEST = static_main AR = ar ARFLAGS = -rsv all: clean $(TEST) $(TEST): $(OBJS) $(LIBOUT) $(CC) $(CPPFLAGS) $(INC) -o $(TEST) $(TEST).cpp -L$(LIBPATH) -l$(LIBNAME) $(LIBNAME).o: $(CC) $(CPPFLAGS) $(INC) -c $(LIBNAME).cpp $(LIBOUT): $(LIBNAME).o $(AR) $(ARFLAGS) $(LIBOUT) $(OBJS) clean: rm -rf $(LIBOUT) $(OBJS) $(TEST)

Output of invoking gmake:

Code:
rm -rf libstatic_lib.a static_lib.o static_main g++ -Wall -W -pedantic -I. -c static_lib.cpp ar -rsv libstatic_lib.a static_lib.o ar: creating libstatic_lib.a a - static_lib.o g++ -Wall -W -pedantic -I. -o static_main static_main.cpp -L. -lstatic_lib

Output of executing static_main:

Code:
3 + 4 = 7

Note that only very minor differences to the Makefile should be required for those who are using Windoze.


:davis:
  #3  
Old 03-Sep-2007, 11:18
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Help With Static Libraries?


Quote:
Originally Posted by Dark dude
So, basically, I'm writing a test static library...+

I suggest that before putting a function into a library that you test the function in a "regular" program.

Here is the function in a C program:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>

int Sum(int i)
{
    return i * 5;
}

int main()
{
    int sumnum;
    scanf("%d", sumnum);
    sumnum = Sum(sumnum);
    printf("%d", sumnum);
    system("PAUSE");
    return 0;
}

If you get a seg fault here, as I did, you know that the problem is with either the main() function or the Sum() function, and not due to some procedural error in putting the function into a library.

I also suggest that you change project settings so that you turn on compiler warnings.

If I compile with -Wall -W -pedantic, the compiler tells me:
Code:
gcc -Wall -W -pedantic test.c -o test test.c: In function ‘main’: test.c:12: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’

Running the program gives me a segfault.

Fix the function. Thoroughly test the function. Then see what you have to do to get it into a library.

This is a trivial function, so testing it is also trivial.

Generally speaking, testing library functions is extremely important. Of course testing is always important, and of course, in general, you can't necessarily prove a function is correct just by testing. But, generally speaking, testing is easier (more time-effective) if you test it before going through the steps of putting it into a library.


Regards,

Dave
  #4  
Old 03-Sep-2007, 11:25
Dark dude Dark dude is offline
New Member
 
Join Date: Sep 2007
Posts: 2
Dark dude is on a distinguished road

Re: Help With Static Libraries?


Edit: Solved it with help of both previous posters
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
triangle (polygon), drawing, sizing, and rotation programme using linked lists... promsan C Programming Language 12 14-May-2007 15:03
Anything i can do with static addActionListener(this); RobRob47 Java Forum 2 26-Mar-2007 13:17
draw in image jafar FLTK Forum 2 19-Sep-2006 11:51
Handling keyup events. harroldm FLTK Forum 3 24-Aug-2006 09:48
Bloodshed Dev C++ Project Options JdS C++ Forum 6 11-Nov-2005 18:23

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:47.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.