Reputation: 421
I've been provided a sample C file and I'd like to break it up into a pair of .c files and a .h file. I'm having trouble getting the program to compile after splitting everything, but I think it's a simple solution. My C is just rusty.
capture.h
#ifndef __CAPTURE_H
#define __CAPTURE_H
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "master.h"
#include "pvcam.h"
static void AcquireStandard( int16 hCam, int16 num_frames, uns32 exp_time );
void setROI( rgn_type* roi, uns16 s1, uns16 s2, uns16 sbin, uns16 p1, uns16 p2, uns16 pbin );
void printROI( int16 roi_count, rgn_type* roi );
void print_pv_error( );
void setFullFrame( int16 hcam, rgn_type* roi );
#endif
capture.c
#include "capture.h"
void AcquireStandard( int16 hCam, int16 num_frames, uns32 exp_time ) {
... Does lots of stuff ...
}
... Many other functions, etc...
pixis.c
#include "capture.h"
int main(int argc, char **argv)
{
char cam_name[CAM_NAME_LEN]; /* camera name */
int16 hCam; /* camera handle */
int16 cam_selection;
int16 num_frames, circ_buff_size, buffer_type;
uns32 port, shtr_open_mode;
uns32 enum_param, exp_time;
int16 adc_index;
int16 gain;
uns16 exp_res_index;
char *s;
..... snip .....
AcquireStandard( hCam, num_frames, exp_time );
pl_cam_close( hCam );
pl_pvcam_uninit();
return 0;
}
I snipped out a lot of irrelevant stuff. When I compile, I get the following error:
gcc -o pixis pixis.o capture.o -I/usr/local/pvcam/examples -lpvcam -ldl -lpthread -lraw1394
pixis.o: In function `main':
pixis.c:(.text+0x14a): undefined reference to `AcquireStandard'
collect2: ld returned 1 exit status
make: *** [pixis] Error 1
AcquireStandard is in the .o file, I checked it with nm:
nm capture.o
00000000 t AcquireStandard
00000000 d _master_h_
00000004 d _pvcam_h_
U fclose
U fopen
U free
U fwrite
U malloc
U pl_error_code
U pl_error_message
U pl_exp_check_status
U pl_exp_finish_seq
U pl_exp_init_seq
U pl_exp_setup_seq
U pl_exp_start_seq
U pl_exp_uninit_seq
U pl_get_param
00000350 T printROI
00000440 T print_pv_error
U printf
U puts
00000489 T setFullFrame
000002d4 T setROI
Now I'm stumped. Did something change in gcc in the last few years or did I just forget some simple detail?
Upvotes: 1
Views: 108
Reputation: 39837
The problem here is that the AcquireStandard()
function was originally prototyped to be static
, meaning its visibility was constrained to the file it was implemented in. In this refactoring, the function's visibility is required to reach out to other sources -- but the header still has the static
keyword on the function.
Notice also the output of the nm
command, specifically the differences between these two lines representing functions in the source:
00000000 t AcquireStandard
00000489 T setFullFrame
The T
indicates a function in the object's text that has global visibility, while the t
tells you the visibility is local.
Remove static
on the prototype in capture.h
and the problem will be solved.
Upvotes: 1