2

I have a simple program use static library for control pci device. I have got some examples. But i want to make myself some examples but i can't link static libraries.

I have 3 file: led.cpp main.cpp main.h

gcc -c led.cpp -I../utils -I../driver -o led.o

gcc -c main.cpp -I../utils -I../driver -o main.o

it' s ok. Succesfuly creating main.o and led.o object files.

But when linking state, its broken. 24dsi20c500k_utils.a and 24dsi20c500k_dsl.a static libraries.

gcc led.o main.o ../utils/24dsi20c500k_utils.a ../docsrc/24dsi20c500k_dsl.a -o led

Output is shown:

led.o: In function `led_tests(int)':
led.cpp:(.text+0x18): undefined reference to `gsc_label(char const*)'
led.cpp:(.text+0x31): undefined reference to `dsi20c500k_initialize(int, int)'
led.cpp:(.text+0x39): undefined reference to `gsc_label_level_inc()'
led.cpp:(.text+0x5b): undefined reference to `dsi20c500k_led(int, int, int, int*)'
led.cpp:(.text+0x81): undefined reference to `dsi20c500k_led(int, int, int, int*)'
led.cpp:(.text+0xa2): undefined reference to `gsc_label_level_dec()'
led.cpp:(.text+0xb1): undefined reference to `dsi20c500k_initialize(int, int)'
main.o: In function `_perform_tests(int)':
main.cpp:(.text+0x3a1): undefined reference to `gsc_label(char const*)'
main.cpp:(.text+0x3c6): undefined reference to `gsc_id_driver(int, char const*)'
main.o: In function `main':
main.cpp:(.text+0x42c): undefined reference to `gsc_label_init(int)'
main.cpp:(.text+0x49a): undefined reference to `gsc_id_host()'
main.cpp:(.text+0x4a4): undefined reference to `gsc_count_boards(char const*)'
main.cpp:(.text+0x4b6): undefined reference to `gsc_select_1_board(int, int*)'
main.cpp:(.text+0x4d7): undefined reference to `gsc_label(char const*)'
main.cpp:(.text+0x500): undefined reference to `gsc_dev_open(unsigned int, char const*)'
main.cpp:(.text+0x54f): undefined reference to `gsc_dev_close(unsigned int, int)'
collect2: ld returned 1 exit status
make: *** [led] Error 1

if i rename cpp files to c files compilation succesfull. What is the problem?

Martin York
  • 257,169
  • 86
  • 333
  • 562
querman
  • 135
  • 1
  • 2
  • 8

2 Answers2

6

gcc -c main.cpp will compile your code as C++ code.

What likely happens then, is that your main.cpp includes other header files, that are not meant to be compiled as C++. This means that when including these header files in a C++ program , gcc will assume all the stuff the header files declares are C++.

And if they're not, you get linker errors if you try to link to C code that the compiler assumed was C++.

You can remedy this by stating that those header files are C. e.g. in your main.cpp you'd do

extern "C" {
#include "some_c_lib.h"
}
nos
  • 223,662
  • 58
  • 417
  • 506
0

You probably didn't use extern "C".

if i rename cpp files to c files compilation succesfull. What is the problem?

Sebastian
  • 1,839
  • 12
  • 16