static library and link errors

Hi Guys

I was doing the porting of my C++ app to linux, when the next error happened. I'm using the Code::Blocks IDE for manage my project.
For ex:
I have one executable project(console application) that uses two static libraries(they contain all application logic).
I have compiled all projects without errors. But i have a problems when linker is working.

for ex, i receive the next error message on linking:
1
2
/home/user/projects/logicstor/lib/uDebug/libclient.a(logic_client.o): In function `logic_client::upload_file_action()':
/home/user/projects/logicstor/client/logic_client.cpp:43: undefined reference to `logic::files_storage::files_storage(std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > const&, logic::files_storage::en_file_storage_type)'

It means that linker couldn't find definition of function "logic::files_storage::files_storage"(in contains in my second library libcommon.a) while it is linking the libclient.a (my first libtary)

i whant to show thу part of code that makes definition and declaration of the "logic::files_storage::files_storage" in libcommon.a:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
files_storage.h
namespace logic
{
	class files_storage
	{
	public:
		enum en_file_storage_type { EN_CLIENT_FILE_STORAGE, EN_SERVER_FILE_STORAGE };

		files_storage(const std::wstring& root_storage_path, en_file_storage_type type);
		~files_storage();
...

files_storage.cpp

namespace logic
{
	files_storage::files_storage(const std::wstring& root_storage_path, files_storage::en_file_storage_type type)
	{
}
...


I have checked my libcommon.a with nm tool, it says that the library exports this function

I don't know what to do, Please help me guys!!!
Thanks
Porting from Windows? Have you explicitly linked against libcommon.a? You need to do this in POSIX, but not Windows.
Thanks for reply, i think that i do the same, i want to show you my makefiles:
I have 3 makefiles:

- makefile for library libcommon.a
- makefile for library libclient.a
- makefile for executable logic_client

makefile for library libcommon.a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#-----------------------------------------------------------------------------
#	Usage of make file
#-----------------------------------------------------------------------------
# Clean operation:
#	make -f MakeCommon clean
#
# Make operation:
#	make -f MakeCommon
#
#


#OBJ = $(SRC:.cpp=.o)
OBJ_DIR = ./obj

OUT_DIR= ../lib
OUT_FILE_NAME = libcommon.a

# include directories
INCLUDES=-I. -I../../depends -I../../depends/zlib

# C++ compiler flags (-g -O2 -Wall)
CCFLAGS =

# compiler
CCC = g++


# Enumerating of every *.cpp as *.o and using that as dependency
$(OUT_FILE_NAME): $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(wildcard *.cpp))
	ar -r -o $(OUT_DIR)/$@ $^

#Compiling every *.cpp to *.o
$(OBJ_DIR)/%.o: %.cpp dircreation
	$(CCC) -c $(INCLUDES) $(CCFLAGS) -o $@  $<

dircreation:
	@mkdir -p $(OUT_DIR)
	@mkdir -p $(OBJ_DIR)

.PHONY : clean
clean:
	rm -f $(OBJ_DIR)/*.o $(OUT_DIR)/$(OUT_FILE_NAME) Makefile.bak
  */


Build Process Output:
1
2
3
4
5
6
7
8
9
g++ -c -I. -I../../depends -I../../depends/zlib  -o obj/files_storage.o  files_storage.cpp
g++ -c -I. -I../../depends -I../../depends/zlib  -o obj/files_storage_exception.o  files_storage_exception.cpp
g++ -c -I. -I../../depends -I../../depends/zlib  -o obj/io_service_pool.o  io_service_pool.cpp
g++ -c -I. -I../../depends -I../../depends/zlib  -o obj/logic_exception_base.o  logic_exception_base.cpp
g++ -c -I. -I../../depends -I../../depends/zlib  -o obj/logic_login_exception.o  logic_login_exception.cpp
g++ -c -I. -I../../depends -I../../depends/zlib  -o obj/thread_pool.o  thread_pool.cpp
g++ -c -I. -I../../depends -I../../depends/zlib  -o obj/torrent_actor_exception.o  torrent_actor_exception.cpp
g++ -c -I. -I../../depends -I../../depends/zlib  -o obj/utils.o  utils.cpp
ar -r -o ../lib/libcommon.a obj/files_storage.o obj/files_storage_exception.o obj/io_service_pool.o obj/logic_exception_base.o obj/logic_login_exception.o obj/thread_pool.o obj/torrent_actor_exception.o obj/utils.o


makefile for library libclient.a

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#-----------------------------------------------------------------------------
#	Usage of make file
#-----------------------------------------------------------------------------
# Clean operation:
#	make -f MakeClient clean
#
# Make operation:
#	make -f MakeClient
#
#


#OBJ = $(SRC:.cpp=.o)
OBJ_DIR = ./obj

OUT_DIR= ../lib
OUT_FILE_NAME = libclient.a

# include directories
INCLUDES=-I. -I../common -I../../depends -I../../depends/zlib

# C++ compiler flags (-g -O2 -Wall)
CCFLAGS =

# compiler
CCC = g++


# Enumerating of every *.cpp as *.o and using that as dependency
$(OUT_FILE_NAME): $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(wildcard *.cpp))
	ar -r -o $(OUT_DIR)/$@ $^

#Compiling every *.cpp to *.o
$(OBJ_DIR)/%.o: %.cpp dircreation
	$(CCC) -c $(INCLUDES) $(CCFLAGS) -o $@ $<

dircreation:
	@mkdir -p $(OUT_DIR)
	@mkdir -p $(OBJ_DIR)

.PHONY : clean
clean:
	rm -f $(OBJ_DIR)/*.o $(OUT_DIR)/$(OUT_FILE_NAME) Makefile.bak
  */


Build Process Output:

1
2
3
4
5
6
7
8
g++ -c -I. -I../common -I../../depends -I../../depends/zlib  -o obj/client_server_interaction.o client_server_interaction.cpp
g++ -c -I. -I../common -I../../depends -I../../depends/zlib  -o obj/client_unique_settings_collector.o client_unique_settings_collector.cpp
g++ -c -I. -I../common -I../../depends -I../../depends/zlib  -o obj/command_line_parser.o command_line_parser.cpp
g++ -c -I. -I../common -I../../depends -I../../depends/zlib  -o obj/connector.o connector.cpp
g++ -c -I. -I../common -I../../depends -I../../depends/zlib  -o obj/logic_client.o logic_client.cpp
g++ -c -I. -I../common -I../../depends -I../../depends/zlib  -o obj/torrent_actor.o torrent_actor.cpp
g++ -c -I. -I../common -I../../depends -I../../depends/zlib  -o obj/transport.o transport.cpp
ar -r -o ../lib/libclient.a obj/client_server_interaction.o obj/client_unique_settings_collector.o obj/command_line_parser.o obj/connector.o obj/logic_client.o obj/torrent_actor.o obj/transport.o



makefile for executable logic_client
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#-----------------------------------------------------------------------------
#	Usage of make file
#-----------------------------------------------------------------------------
# Clean operation:
#	make -f MakeLogic clean
#
# Make operation:
#	make -f MakeLogic
#
#


#OBJ = $(SRC:.cpp=.o)
OBJ_DIR = ./obj

LIB_DIR = -L../lib -L../../../buildlib/boost_1_40_0/stage/lib/debug
LIBS = -lcommon -lclient -lboost_program_options

OUT_DIR= ../bin
OUT_FILE_NAME = logic_client

# include directories
INCLUDES=-I. -I../common -I../client -I../../depends -I../../depends/zlib

# C++ compiler flags (-g -O2 -Wall)
CCFLAGS =

# compiler
CCC = g++


# Enumerating of every *.cpp as *.o and using that as dependency
$(OUT_FILE_NAME): $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(wildcard *.cpp))
	$(CCC) -static $(LIB_DIR) $(LIBS) -o $(OUT_DIR)/$@ $^

#Compiling every *.cpp to *.o
$(OBJ_DIR)/%.o: %.cpp dircreation
	$(CCC) -c $(INCLUDES) $(CCFLAGS) -o $@ $<

dircreation:
	@mkdir -p $(OUT_DIR)
	@mkdir -p $(OBJ_DIR)

.PHONY : clean
clean:
	rm -f $(OBJ_DIR)/*.o $(OUT_DIR)/$(OUT_FILE_NAME) Makefile.bak
  */



Build Process Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
g++ -c -I. -I../common -I../client -I../../depends -I../../depends/zlib  -o obj/logic_client_app.o logic_client_app.cpp
g++ -c -I. -I../common -I../client -I../../depends -I../../depends/zlib  -o obj/stdafx.o stdafx.cpp
g++ -static -L../lib -L../../../buildlib/boost_1_40_0/stage/lib/debug -lcommon -lboost_program_options -o ../bin/logic_client obj/logic_client_app.o obj/stdafx.o

In file included from ../common/utils.h:4:0,
                 from ../client/common_client_includes.h:15,
                 from ../client/logic_client.h:4,
                 from logic_client_app.cpp:7:
../common/protocol.h:14:28: warning: scoped enums only available with -std=c++0x or -std=gnu++0x [enabled by default]
obj/logic_client_app.o: In function `main':
logic_client_app.cpp:(.text+0xbe): undefined reference to `command_line_parser(int, char**)'
logic_client_app.cpp:(.text+0xd1): undefined reference to `logic_client::logic_client(settings const&)'
logic_client_app.cpp:(.text+0xdd): undefined reference to `logic_client::do_work()'
logic_client_app.cpp:(.text+0xe9): undefined reference to `logic_client::~logic_client()'
logic_client_app.cpp:(.text+0x115): undefined reference to `logic_client::~logic_client()'
obj/logic_client_app.o: In function `__static_initialization_and_destruction_0(int, int)':
logic_client_app.cpp:(.text+0x1f4): undefined reference to `boost::system::get_system_category()'
logic_client_app.cpp:(.text+0x200): undefined reference to `boost::system::get_generic_category()'
logic_client_app.cpp:(.text+0x20c): undefined reference to `boost::system::get_generic_category()'


That is my problem, do you have any idea?

Thanks
Last edited on
You haven't shown the output from building libcommon.

What file is files_storage in?

The program doesn't link against libclient.
I have edited the my post, added output from building libcommon
Have you added libclient to the program's lib section?
I am sorry, it was my mistake, now the MakeLogic is corrected, the -lclient flag always has been presented in my sources. I forgot to add it only in my message.
Hi Guys
I have found the cause of the problem!!!

When we link library:

1
2
3
4
# Enumerating of every *.cpp as *.o and using that as dependency
$(OUT_FILE_NAME): $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(wildcard *.cpp))
	$(CCC) -static $(LIB_DIR) $(LIBS) -o $(OUT_DIR)/$@ $^


we need move " -static $(LIB_DIR) $(LIBS)" to the end of string i.e.:

1
2
3
4
# Enumerating of every *.cpp as *.o and using that as dependency
$(OUT_FILE_NAME): $(patsubst %.cpp,$(OBJ_DIR)/%.o,$(wildcard *.cpp))
	$(CCC)  -o $(OUT_DIR)/$@ $^ -static $(LIB_DIR) $(LIBS)


it has resolved my issue.

Thanks

Topic archived. No new replies allowed.