bachra04
asked on
conditional output in makefile
Hi All,
I have the following code in my config.h
#define SERVER_MODE 1
Then I want the make file to test for this value in order to generate the output
Sthg like that :
Ifeq (SERVER_MODE,1)
à my outout is server
Else
My output is client
Endif
Can you help to get the right syntax
Im running my code on Sun 5.10
I have the following code in my config.h
#define SERVER_MODE 1
Then I want the make file to test for this value in order to generate the output
Sthg like that :
Ifeq (SERVER_MODE,1)
à my outout is server
Else
My output is client
Endif
Can you help to get the right syntax
Im running my code on Sun 5.10
Usually, you'd define the SERVER_MODE in the makefile, and pass it to the compiler using -DSERVER_MODE=1
ASKER
The problem is that I use SERVER_MODE inside my main.cpp class
You could do something like:
!if "$(CFG)" == "SERVER"
#statements for server build
!endif
!if "$(CFG)" == "CLIENT"
#statements for client build
!endif
Then pass the configuration when calling make (nmake at windows) by adding CFG=SERVER to the call.
!if "$(CFG)" == "SERVER"
#statements for server build
!endif
!if "$(CFG)" == "CLIENT"
#statements for client build
!endif
Then pass the configuration when calling make (nmake at windows) by adding CFG=SERVER to the call.
>> The problem is that I use SERVER_MODE inside my main.cpp class
That's ok. By passing the define using -DSERVER_MODE=1, it will be available in the main.cpp file. It would be as if you defined it in the file itself.
That's ok. By passing the define using -DSERVER_MODE=1, it will be available in the main.cpp file. It would be as if you defined it in the file itself.
To read the SERVER_MODE from header file you could use a grep (or find at Windows) and depending on the result call make with client or server option.
ASKER
>>That's ok. By passing the define using -DSERVER_MODE=1, it will be available in the main.cpp file. It would be as if you defined it in the file itself.
Is it possible to write code inside the Make file so I extract the value of SERVER_MODE by simply including config.h inside my makefile ?
Is it possible to write code inside the Make file so I extract the value of SERVER_MODE by simply including config.h inside my makefile ?
>> Is it possible to write code inside the Make file so I extract the value of SERVER_MODE by simply including config.h inside my makefile ?
That's the reverse world. The makefile defines what kind of application you build. The config.h file should be used for more general configuration settings (general to both client and server).
A simple example makefile (just to illustrate what I mean) is below. It is either called like :
make server
or :
make client
depending on which you want to build.
That's the reverse world. The makefile defines what kind of application you build. The config.h file should be used for more general configuration settings (general to both client and server).
A simple example makefile (just to illustrate what I mean) is below. It is either called like :
make server
or :
make client
depending on which you want to build.
## makefile ##
server : main.cpp
CC -DSERVER_MODE=1 main.cpp -o server
client : main.cpp
CC -DSERVER_MODE=0 main.cpp -o client
ASKER
I agree with you but we have a building framework and I only have the right to change the makefile inside my directory (I don't have direct access to gcc options)
#OBJ_DIR = ./$(SRV_BUILD)/$(COMPILER)
#ADD_FLAGS = -I. -I..
#include ../common.mk
#include ./files.mk
#include ./CProtocolConfig.h
#SHARED_LIB = $(LIB_DIR)/libProtocols-$( LIBSSUFF)
#all: $(SHARED_LIB)
#clean: clean_sharedls
#clobber: clobber_shared
#include ../common_targets.mk
#-include $(DEP_FILES)
OBJ_DIR = ./$(SRV_BUILD)/$(COMPILER)
BIN_DIR = ./bin/$(SRV_BUILD)/$(COMPI LER)
ADD_FLAGS = -I. -I..
include ../common.mk
include ./files.mk
#ifeq(SERVER_MODE, 1)
#GWSRV = $(BIN_DIR)/QmtfServer
#else
GWSRV = $(BIN_DIR)/QmtfClient
#endif
SOL_COMMON = -lCommon-$(LIBSUFF)
#SOL_DBACC = -lDBAccess-$(LIBSUFF)
#SOL_PROTO = -lProtocols-$(LIBSUFF)
#SOL_FIX = -lFix-$(LIBSUFF)
SOLIBS = -L$(LIB_DIR) $(SOL_COMMON)
all: $(GWSRV)
$(GWSRV) : $(BIN_DIR) $(OBJ_DIR) $(ARLIBS) $(OBJ_FILES)
$(CCC) $(COMMON_EXEC) $(OBJ_FILES) $(ARLIBS) $(EXEC_FLAGS) $(BOOST_LINK) $(SOLIBS) -o $(GWSRV)
clean:
rm -f $(GWSRV) $(OBJ_FILES) $(DEP_FILES)
clobber:
rm -rf $(OBJ_DIR) $(BIN_DIR)
include ../common_targets.mk
-include $(DEP_FILES)
#OBJ_DIR = ./$(SRV_BUILD)/$(COMPILER)
#ADD_FLAGS = -I. -I..
#include ../common.mk
#include ./files.mk
#include ./CProtocolConfig.h
#SHARED_LIB = $(LIB_DIR)/libProtocols-$(
#all: $(SHARED_LIB)
#clean: clean_sharedls
#clobber: clobber_shared
#include ../common_targets.mk
#-include $(DEP_FILES)
OBJ_DIR = ./$(SRV_BUILD)/$(COMPILER)
BIN_DIR = ./bin/$(SRV_BUILD)/$(COMPI
ADD_FLAGS = -I. -I..
include ../common.mk
include ./files.mk
#ifeq(SERVER_MODE, 1)
#GWSRV = $(BIN_DIR)/QmtfServer
#else
GWSRV = $(BIN_DIR)/QmtfClient
#endif
SOL_COMMON = -lCommon-$(LIBSUFF)
#SOL_DBACC = -lDBAccess-$(LIBSUFF)
#SOL_PROTO = -lProtocols-$(LIBSUFF)
#SOL_FIX = -lFix-$(LIBSUFF)
SOLIBS = -L$(LIB_DIR) $(SOL_COMMON)
all: $(GWSRV)
$(GWSRV) : $(BIN_DIR) $(OBJ_DIR) $(ARLIBS) $(OBJ_FILES)
$(CCC) $(COMMON_EXEC) $(OBJ_FILES) $(ARLIBS) $(EXEC_FLAGS) $(BOOST_LINK) $(SOLIBS) -o $(GWSRV)
clean:
rm -f $(GWSRV) $(OBJ_FILES) $(DEP_FILES)
clobber:
rm -rf $(OBJ_DIR) $(BIN_DIR)
include ../common_targets.mk
-include $(DEP_FILES)
>>>> That's the reverse world.
Infinity is right. A header is a bad place to store build options. You would need to change the header when building both client and server at one system. Or, if using a version and configuration system to manage sources between systems, you would need to have different active versions of the same header.
Better pass the SERVER_MODE as compiler option as Infinity explained in his first comment.
Infinity is right. A header is a bad place to store build options. You would need to change the header when building both client and server at one system. Or, if using a version and configuration system to manage sources between systems, you would need to have different active versions of the same header.
Better pass the SERVER_MODE as compiler option as Infinity explained in his first comment.
>> I don't have direct access to gcc options)
The gcc options are passed in the makefile. So, you just have to modify the makefile. See my example.
The gcc options are passed in the makefile. So, you just have to modify the makefile. See my example.
>>>> I only have the right to change the makefile inside my directory
Then, you either should provide 2 makefiles where one was building the client and another builds the server or you build both the client and the server in the makefile.
>>> (I don't have direct access to gcc options)
You have. Add the -DSERVER_MODE=1 to the statement beginning with $(CCC)
Then, you either should provide 2 makefiles where one was building the client and another builds the server or you build both the client and the server in the makefile.
>>> (I don't have direct access to gcc options)
You have. Add the -DSERVER_MODE=1 to the statement beginning with $(CCC)
ASKER
You're right
I can do that but if you see I want my output name once called qmtfServer and another time called QmtfClient based on the value of only one variable
#GWSRV = $(BIN_DIR)/QmtfServer
#else
GWSRV = $(BIN_DIR)/QmtfClient
I can do that but if you see I want my output name once called qmtfServer and another time called QmtfClient based on the value of only one variable
#GWSRV = $(BIN_DIR)/QmtfServer
#else
GWSRV = $(BIN_DIR)/QmtfClient
How about :
OBJ_DIR = ./$(SRV_BUILD)/$(COMPILER)
BIN_DIR = ./bin/$(SRV_BUILD)/$(COMPILER)
ADD_FLAGS = -I. -I..
include ../common.mk
include ./files.mk
SERVER_BIN = $(BIN_DIR)/QmtfServer
CLIENT_BIN = $(BIN_DIR)/QmtfClient
SOL_COMMON = -lCommon-$(LIBSUFF)
#SOL_DBACC = -lDBAccess-$(LIBSUFF)
#SOL_PROTO = -lProtocols-$(LIBSUFF)
#SOL_FIX = -lFix-$(LIBSUFF)
SOLIBS = -L$(LIB_DIR) $(SOL_COMMON)
all : server client
server : $(SERVER_BIN)
client : $(CLIENT_BIN)
$(SERVER_BIN) : $(BIN_DIR) $(OBJ_DIR) $(ARLIBS) $(OBJ_FILES)
$(CCC) -DSERVER_MODE=1 $(COMMON_EXEC) $(OBJ_FILES) $(ARLIBS) $(EXEC_FLAGS) $(BOOST_LINK) $(SOLIBS) -o $(SERVER_BIN)
$(CLIENT_BIN) : $(BIN_DIR) $(OBJ_DIR) $(ARLIBS) $(OBJ_FILES)
$(CCC) -DSERVER_MODE=0 $(COMMON_EXEC) $(OBJ_FILES) $(ARLIBS) $(EXEC_FLAGS) $(BOOST_LINK) $(SOLIBS) -o $(CLIENT_BIN)
clean:
rm -f $(SERVER_BIN) $(CLIENT_BIN) $(OBJ_FILES) $(DEP_FILES)
clobber:
rm -rf $(OBJ_DIR) $(BIN_DIR)
include ../common_targets.mk
-include $(DEP_FILES)
If you want to build the server, you call :
make server
If you want to build the client :
make client
Couldn't be simpler ;)
make server
If you want to build the client :
make client
Couldn't be simpler ;)
ASKER
No couldn't be simpler
but inside my main class how to test the SERVER_MODE value ?
using #if ?
but inside my main class how to test the SERVER_MODE value ?
using #if ?
>> but inside my main class how to test the SERVER_MODE value ?
The same way you are doing now :) Just treat it as if it was #define'd.
>> using #if ?
For example, yes.
The same way you are doing now :) Just treat it as if it was #define'd.
>> using #if ?
For example, yes.
ASKER
I got the client and server output. but only the name is changing. looks like it does not take the SERVER_MODE value
#if SERVER_MODE returns usually false
#if SERVER_MODE returns usually false
>> returns usually false
What do you mean by "usually" ?
>> looks like it does not take the SERVER_MODE value
Can you show your source code ? Did you use the makefile I posted ? Did you remove the #define from config.h ?
What do you mean by "usually" ?
>> looks like it does not take the SERVER_MODE value
Can you show your source code ? Did you use the makefile I posted ? Did you remove the #define from config.h ?
ASKER
yes I have remove the
#define from config.h ?
#if SERVER_MODE
if (argc < 3)
{
std::cerr << "Usage: QmtfServer <host> <port> \n";
return 1;
}
ORBIXA_PREPARE_COMM_SESSIO N
CProtocolInfo cInfo(ORBIXA_IO_COMM);
cInfo.setHostAddress(argv[ 1]);
cInfo.setHostPort(boost::l exical_cas t<unsigned int>(argv[2]));
cInfo.setProtocol(IProtoco lInfo::eTC P_IP);
cInfo.setMode(IProtocolInf o::eServer );
cInfo.setChannelNumber(1);
IInternalProtocol* pInternalProtocol =
CInternalProtocolFactory:: getInstanc e()->getIn ternalProt ocol(&cInf o);
IInternalProtocol::protoco ls pvecMeProtocol;
pInternalProtocol->getProc essProtoco l(IInterna lProtocol: :PROC_ME, pvecMeProtocol);
// Set and hook the handler
pvecMeProtocol[0]->setProt ocolHandle r<CMyProto colHandler >();
pvecMeProtocol[0]->hookHan dlers();
ORBIXA_START_COMM_SESSION
char line[qmtf_message::max_bod y_length + 1];
while (std::cin.getline(line, qmtf_message::max_body_len gth + 1))
{
using namespace std; // For strlen and memcpy.
qmtf_message msg;
msg.setType(7);
msg.body_length(strlen(lin e));
memcpy(msg.body(), line, msg.body_length());
msg.encode_header();
pvecMeProtocol[0]->writeQu eue( 0, 7, msg.body_./length(), (const void*) msg.body());
}
ORBIXA_END_COMM_SESSION
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
#else
//======================== ======
//
// Create a TCP client
//
//======================== ======
if (argc != 3)
#define from config.h ?
#if SERVER_MODE
if (argc < 3)
{
std::cerr << "Usage: QmtfServer <host> <port> \n";
return 1;
}
ORBIXA_PREPARE_COMM_SESSIO
CProtocolInfo cInfo(ORBIXA_IO_COMM);
cInfo.setHostAddress(argv[
cInfo.setHostPort(boost::l
cInfo.setProtocol(IProtoco
cInfo.setMode(IProtocolInf
cInfo.setChannelNumber(1);
IInternalProtocol* pInternalProtocol =
CInternalProtocolFactory::
IInternalProtocol::protoco
pInternalProtocol->getProc
// Set and hook the handler
pvecMeProtocol[0]->setProt
pvecMeProtocol[0]->hookHan
ORBIXA_START_COMM_SESSION
char line[qmtf_message::max_bod
while (std::cin.getline(line, qmtf_message::max_body_len
{
using namespace std; // For strlen and memcpy.
qmtf_message msg;
msg.setType(7);
msg.body_length(strlen(lin
memcpy(msg.body(), line, msg.body_length());
msg.encode_header();
pvecMeProtocol[0]->writeQu
}
ORBIXA_END_COMM_SESSION
}
catch (std::exception& e)
{
std::cerr << "Exception: " << e.what() << "\n";
}
#else
//========================
//
// Create a TCP client
//
//========================
if (argc != 3)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes I'm thinking about putting both client and server in different files
You easily can check wether an #if applies by adding a
#error "any text"
after the #if statement
The precompiler would stop at that statement and printout the message.
#error "any text"
after the #if statement
The precompiler would stop at that statement and printout the message.
ASKER